Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Append object to JSON

Tags:

java

json

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

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

      }
    ]

I'm trying to do it using the code in example, but unfortunately whole object is overriden everytime.

    Log.i(AppHelper.APP_LOG_NAMESPACE, "POSITIONS AVAILABLE " + jsonDataString);
                AppHelper helper = new AppHelper(ctx);
                JSONObject mainObject = new JSONObject(jsonDataString);
                JSONObject valuesObject = new JSONObject();
                JSONArray list = new JSONArray();
                //putv given values to the JSON
                valuesObject.put("lat", lat.toString());
                valuesObject.put("lon", lon.toString());
                valuesObject.put("city", city);
                valuesObject.put("street", street);
                valuesObject.put("date", helper.getActualDateTime());
                valuesObject.put("time", helper.getActualDateTime());
                list.put(valuesObject);
                //mainObject.put("values", list);
                mainObject.accumulate("values", list);
                saveJsonData(ctx, mainObject.toString(),"positions");

How it should be right?

Put and accumulate everytime rewrite all previous values, but i would like to append this object:

{
     "lat":"value",
     "lon":"value"
  },

Into results parent.

BTW: I would like to do it without GSON.

Thanks for any help..

like image 604
redrom Avatar asked May 18 '14 16:05

redrom


2 Answers

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);
    }

}
like image 120
Syam S Avatar answered Sep 29 '22 19:09

Syam S


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"}]}
like image 21
Munish Kapoor Avatar answered Sep 29 '22 19:09

Munish Kapoor