How to maintain the order of a JSONObject

后端 未结 12 1630
栀梦
栀梦 2020-12-11 05:49

I am using a JSONObject in order to remove a certin attribute I don\'t need in a JSON String:

JSONObject jsonObject = new JSONObject(jsonString);
jsonObject.         


        
12条回答
  •  不知归路
    2020-12-11 06:17

    If you can edit the server repose then change it to array of JSON objects.

    JSON:

    [
    {PropertyName:"Date of Issue:",PropertyValue:"3/21/2011"},
    PropertyName:"Report No:",PropertyValue:"2131196186"},{PropertyName:"Weight:",PropertyValue:"1.00"},
    {PropertyName:"Report Type:",PropertyValue:"DG"}
    ]
    

    And I handled it with JSONArray in client side (Android):

    String tempresult="[{PropertyName:"Date of Issue:",PropertyValue:"3/21/2011"},PropertyName:"Report No:",PropertyValue:"2131196186"},PropertyName:"Weight:",PropertyValue:"1.00"},{PropertyName:"Report Type:",PropertyValue:"DG"}]"
    
    JSONArray array = new JSONArray(tempresult);
                 for (int i = 0; i < array.length(); i++) 
                 {
                     String key = array.getJSONObject(i).getString("PropertyName"); 
                     String value = array.getJSONObject(i).getString("PropertyValue");
                     rtnObject.put(key.trim(),value.trim()); //rtnObject is LinkedHashMap but can be any other object which can keep order.
    
    
             }
    

提交回复
热议问题