Search code examples
javajsonstruts2struts2-json-pluginstruts-action

How To return only array not json object in struts2


<action name="commissioner_info" method="commissioner_info" class="foo.bar.AdminInfoAction">
    <result name="json" type="json">    
        <param name="includeProperties">
            commissioner_info\[\d+\]\.fname,
            commissioner_info\[\d+\]\.designation,        
            commissioner_info\[\d+\]\.pathlocation
        </param>
    </result>
</action>

Above code giving Result:

{"commissioner_info":[{"designation":"IG1","fname":"BS  Bassi ","pathlocation":"http:\/\/103.231.125.106\/nesos\/imagerepresentatives\/151021104556Bhim.jpg"}]}

But I want only array not json object,like

[{"designation":"IG1","fname":"BS  Bassi ","pathlocation":"http:\/\/103.231.125.106\/nesos\/imagerepresentatives\/151021104556Bhim.jpg"}]

Solution

  • You need to specify the object you want to be serialized as the root object:

    <result name="json" type="json">    
        <param name="root">
            commissioner_info
        </param>
    </result>
    

    Otherwise the whole action will be serialized. Read more here.