How to ignore JSON root node in struts2 action
I added this method to my binding activity,
public String execute() {
long start = System.currentTimeMillis();
simDetails = new SIMDetails();
return GET_SIM_DETAILS;
}
and added below in struts config file,
<result type="json" name="getSIMDetails">
<param name="noCache">true</param>
<param name="includeProperties">simDetails.*</param>
</result>
Then I got below JSON response
{
"simDetails": {
"void": null,
"ban": null,
"currentTariff": null,
"currentTariffDescription": null,
"defaultTariff": null,
"defaultTariffDescription": null,
"imsi": null,
"packageItemId": null,
"simSerialNumber": null,
"simStatus": null,
"simStatusCC": null,
"status": null,
"subscriberNumber": null,
"subsidaryCode": null
}
}
but I need this answer, not above,
{
"void": null,
"ban": null,
"currentTariff": null,
"currentTariffDescription": null,
"defaultTariff": null,
"defaultTariffDescription": null,
"imsi": null,
"packageItemId": null,
"simSerialNumber": null,
"simStatus": null,
"simStatusCC": null,
"status": null,
"subscriberNumber": null,
"subsidaryCode": null
}
Any idea to get the required answer without adding add the above field to my action class.
source to share
You can use the attribute root
as stated in the Root Object section of the documentation:
Use the "root" attribute (OGNL expression) to specify the root object to be serialized.
In your case:
<result type="json" name="getSIMDetails">
<param name="noCache">true</param>
<param name="root">simDetails</param>
</result>
PS: this answer might be worth reading. And in another answer to this question, you can also see the Stream method suggested by @IntelliData.
source to share
To avoid your problem, I usually return JSON using the following struts.xml (as opposed to the JSON return type):
<result name="success" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
I am storing an "inputStream" variable of type "InputStream" in my action class, and in the execute () method, I manually assign the JSON "inputStream". This allows me to customize the JSON exactly the way I want and this is exactly what the inputStream returns.
Hope it helps!
source to share