How to maintain the order of a JSONObject

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

    I accomplished it by doing a:

    JSONObject(json).put(key, ObjectMapper().writeValueAsString(ObjectMapper().readValue(string, whatever::class)))

    So essentially I deserialize a string to an ordered class, then I serialize it again. But then I also had to format that string afterwards to remove escapes.

    .replace("\\\"", "\"").replace("\"{", "{").replace("}\"", "}")

    You may also have to replace null items as well if you don't want nulls.

提交回复
热议问题