JSON order mixed up

前端 未结 12 1324
感情败类
感情败类 2020-11-22 02:01

I\'ve a problem trying to make my page printing out the JSONObject in the order i want. In my code, I entered this:



        
12条回答
  •  死守一世寂寞
    2020-11-22 02:36

    For Java code, Create a POJO class for your object instead of a JSONObject. and use JSONEncapsulator for your POJO class. that way order of elements depends on the order of getter setters in your POJO class. for eg. POJO class will be like

    Class myObj{
    String userID;
    String amount;
    String success;
    // getter setters in any order that you want
    

    and where you need to send your json object in response

    JSONContentEncapsulator JSONObject = new JSONEncapsulator("myObject");
    JSONObject.setObject(myObj);
    return Response.status(Status.OK).entity(JSONObject).build();
    

    The response of this line will be

    {myObject : {//attributes order same as getter setter order.}}

提交回复
热议问题