javajsonstruts2struts2-json-pluginstruts-action

How to ignore the root JSON node in struts2 action


I added this method in my struts action,

public String execute() {
    long start = System.currentTimeMillis();
    simDetails = new SIMDetails();
    return GET_SIM_DETAILS;
}

and added below action 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 response instead of the 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 response with out add the above field to my action class.


Solution

  • You can use the root attribute as specified 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>
    

    P.S: this answer might be worthy of a read. And in the other answer to that question you can also see the Stream technique suggested by @IntelliData.