How to maintain the order of a JSONObject

后端 未结 12 1626
栀梦
栀梦 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:40

    You can go for the JsonObject provided by the com.google.gson it is nearly the same with the JSONObject by org.json but some different functions. For converting String to Json object and also maintains the order you can use:

    Gson gson = new Gson();
    JsonObject jsonObject = gson.fromJson(, JsonObject.class);
    

    For eg:-

    String jsonString = "your json String";
    
    JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class);
    

    It just maintains the order of the JsonObject from the String.

提交回复
热议问题