Java 将对象附加到 JSON

red*_*rom 5 java json

我想将 JSON 对象附加到现有的 JSON 数组以获得这样的数据结构。

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

      }
    ]
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用示例中的代码来做到这一点,但不幸的是,每次都覆盖整个对象。

    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");
Run Code Online (Sandbox Code Playgroud)

应该怎么做才对?

每次重写所有以前的值时都进行放置和累积,但我想附加这个对象:

{
     "lat":"value",
     "lon":"value"
  },
Run Code Online (Sandbox Code Playgroud)

进入结果父级。

顺便说一句:我想在没有 GSON 的情况下做到这一点。

谢谢你的帮助..

Sya*_*m S 5

您的代码没有任何问题。它确实附加

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);
Run Code Online (Sandbox Code Playgroud)

这打印{"values":[[{"date":"newValue","city":"newValue","street":"newValue","lon":"newValue","time":"newValue","lat":"newValue"}]],"results":[{"lon":"value","lat":"value"},{"lon":"value","lat":"value"}]}. 这不正是你所期待的吗?

使用 gson 你可以像

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

}
Run Code Online (Sandbox Code Playgroud)