Java将对象附加到JSON

Java将对象附加到JSON

本文介绍了Java将对象附加到JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

      }
    ]

我正在尝试使用示例中的代码来执行此操作,但是不幸的是,每次都覆盖整个对象.

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

应该如何正确?

每次都放入并累积重写所有先前的值,但是我想附加此对象:

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

放入结果父项.

顺便说一句:我想在没有GSON的情况下这样做.

感谢您的帮助.

解决方案

您的代码没有任何问题.它确实会添加

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

这将打印{"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);
    }

}

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..

解决方案

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

}

这篇关于Java将对象附加到JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:50