如何使用JSONObject在Java中创建正确的JsonArray

时间:2021-07-08 19:40:50

how can I create a JSON Object like the following, in Java using JSONObject ?

如何使用JSONObject在Java中创建如下的JSON对象?

{
    "employees": [
        {"firstName": "John", "lastName": "Doe"}, 
        {"firstName": "Anna", "lastName": "Smith"}, 
        {"firstName": "Peter", "lastName": "Jones"}
    ],
    "manager": [
        {"firstName": "John", "lastName": "Doe"}, 
        {"firstName": "Anna", "lastName": "Smith"}, 
        {"firstName": "Peter", "lastName": "Jones"}
    ]
}

I've found a lot of example, but not my exactly JSONArray string.

我找到了很多例子,但不是我的JSONArray字符串。

2 个解决方案

#1


204  

Here is some code using java 6 to get you started:

下面是一些使用java 6启动的代码:

JSONObject jo = new JSONObject();
jo.put("firstName", "John");
jo.put("lastName", "Doe");

JSONArray ja = new JSONArray();
ja.put(jo);

JSONObject mainObj = new JSONObject();
mainObj.put("employees", ja);

Edit: Since there has been a lot of confusion about put vs add here I will attempt to explain the difference. In java 6 org.json.JSONArray contains the put method and in java 7 javax.json contains the add method.

编辑:既然有很多关于put vs add的困惑,我将尝试解释其中的区别。在java 6 org.json。JSONArray包含put方法和java 7 javax。json包含add方法。

An example of this using the builder pattern in java 7 looks something like this:

在java 7中使用构建器模式的示例如下:

JsonObject jo = Json.createObjectBuilder()
  .add("employees", Json.createArrayBuilder()
    .add(Json.createObjectBuilder()
      .add("firstName", "John")
      .add("lastName", "Doe")))
  .build();

#2


10  

I suppose you're getting this JSON from a server or a file, and you want to create a JSONArray object out of it.

我假设您是从服务器或文件中得到这个JSON,您想要创建一个JSONArray对象。

String strJSON = ""; // your string goes here
JSONArray jArray = (JSONArray) new JSONTokener(strJSON).nextValue();
// once you get the array, you may check items like
JSONOBject jObject = jArray.getJSONObject(0);

Hope this helps :)

希望这有助于:)

#1


204  

Here is some code using java 6 to get you started:

下面是一些使用java 6启动的代码:

JSONObject jo = new JSONObject();
jo.put("firstName", "John");
jo.put("lastName", "Doe");

JSONArray ja = new JSONArray();
ja.put(jo);

JSONObject mainObj = new JSONObject();
mainObj.put("employees", ja);

Edit: Since there has been a lot of confusion about put vs add here I will attempt to explain the difference. In java 6 org.json.JSONArray contains the put method and in java 7 javax.json contains the add method.

编辑:既然有很多关于put vs add的困惑,我将尝试解释其中的区别。在java 6 org.json。JSONArray包含put方法和java 7 javax。json包含add方法。

An example of this using the builder pattern in java 7 looks something like this:

在java 7中使用构建器模式的示例如下:

JsonObject jo = Json.createObjectBuilder()
  .add("employees", Json.createArrayBuilder()
    .add(Json.createObjectBuilder()
      .add("firstName", "John")
      .add("lastName", "Doe")))
  .build();

#2


10  

I suppose you're getting this JSON from a server or a file, and you want to create a JSONArray object out of it.

我假设您是从服务器或文件中得到这个JSON,您想要创建一个JSONArray对象。

String strJSON = ""; // your string goes here
JSONArray jArray = (JSONArray) new JSONTokener(strJSON).nextValue();
// once you get the array, you may check items like
JSONOBject jObject = jArray.getJSONObject(0);

Hope this helps :)

希望这有助于:)