Java Append object to JSON

后端 未结 3 1398
深忆病人
深忆病人 2021-01-19 07:13

I would like to append JSON object to existing JSON array to get data structure like this.

\"results\":[
      {
         \"lat\":\"value\",
         \"lon\"         


        
相关标签:
3条回答
  • 2021-01-19 07:43

    If you want to add new value to an Object you can try the below as well

    Before:

    {
        "Name": "EnCoMa",
        "Manager": "Abhishek Kasetty"
     }
    

    code :

    JsonFactory factory = new JsonFactory();      
    ObjectMapper mapper = new ObjectMapper(factory);
        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
        String json = ow.writeValueAsString(TheObjectToWhichYouWantToAddNewValue);
        ObjectNode node = (ObjectNode) mapper.readTree(json);
        node.putPOJO("new Key","new value")
    

    after:

    {
        "Name": "EnCoMa",
        "Manager": "Abhishek Kasetty",
        "new Key": "new value"
    }
    
    0 讨论(0)
  • 2021-01-19 07:45

    There isnt any problem with your code. It does append

    String jsonDataString = "{\"results\":[{\"lat\":\"value\",\"lon\":\"value\" }, { \"lat\":\"value\", \"lon\":\"value\"}]}";
    JSONObject mainObject = new JSONObject(jsonDataString);
    JSONObject valuesObject = new JSONObject();
    JSONArray list = new JSONArray();
    valuesObject.put("lat", "newValue");
    valuesObject.put("lon", "newValue");
    valuesObject.put("city", "newValue");
    valuesObject.put("street", "newValue");
    valuesObject.put("date", "newValue");
    valuesObject.put("time", "newValue");
    list.put(valuesObject);
    mainObject.accumulate("values", list);
    System.out.println(mainObject);
    

    This prints {"values":[[{"date":"newValue","city":"newValue","street":"newValue","lon":"newValue","time":"newValue","lat":"newValue"}]],"results":[{"lon":"value","lat":"value"},{"lon":"value","lat":"value"}]}. Isnt this what you are expecting?

    With gson you can do like

    import com.google.gson.Gson;
    import com.google.gson.JsonObject;
    
    
    public class AddJson {
    
        public static void main(String[] args) {
            String json = "{\"results\":[{\"lat\":\"value\",\"lon\":\"value\" }, { \"lat\":\"value\", \"lon\":\"value\"}]}";
            Gson gson = new Gson();
            JsonObject inputObj  = gson.fromJson(json, JsonObject.class);
            JsonObject newObject = new JsonObject() ;
            newObject.addProperty("lat", "newValue");
            newObject.addProperty("lon", "newValue");
            inputObj.get("results").getAsJsonArray().add(newObject);
            System.out.println(inputObj);
        }
    
    }
    
    0 讨论(0)
  • 2021-01-19 07:47

    Simple Approach

        String jsonData = "{\"results\":[{\"lat\":\"value\",\"lon\":\"value\" }]}";
        System.out.println(jsonData);
        try {
            JSONArray result = new JSONObject(jsonData).getJSONArray("results");
            result.getJSONObject(0).put("city","Singapore");
            jsonData = "{\"results\":"+result.toString()+"}";
            System.out.println(jsonData);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    

    OutPut Before Appending

    {"results":[{"lat":"value","lon":"value" }]} 
    

    OutPut After Appending

    {"results":[{"lon":"value","lat":"value","city":"Singapore"}]}
    
    0 讨论(0)
提交回复
热议问题