如何使用 JSONObject 在 Java 中创建正确的 JSONArray
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18983185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create correct JSONArray in Java using JSONObject
提问by user2010955
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 字符串。
回答by Grammin
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.JSONArraycontains the put
method and in java 7 javax.jsoncontains 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();
回答by Naeem A. Malik
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 :)
希望这可以帮助 :)
回答by Manasi
Small reusable method can be writtenfor creating person json object to avoid duplicate code
可以编写小的可重用方法来创建person json对象以避免重复代码
JSONObject getPerson(String firstName, String lastName){
JSONObject person = new JSONObject();
person .put("firstName", firstName);
person .put("lastName", lastName);
return person ;
}
public JSONObject getJsonResponse(){
JSONArray employees = new JSONArray();
employees.put(getPerson("John","Doe"));
employees.put(getPerson("Anna","Smith"));
employees.put(getPerson("Peter","Jones"));
JSONArray managers = new JSONArray();
managers.put(getPerson("John","Doe"));
managers.put(getPerson("Anna","Smith"));
managers.put(getPerson("Peter","Jones"));
JSONObject response= new JSONObject();
response.put("employees", employees );
response.put("manager", managers );
return response;
}
回答by MSD
Please try this ... hope it helps
请试试这个...希望它有帮助
JSONObject jsonObj1=null;
JSONObject jsonObj2=null;
JSONArray array=new JSONArray();
JSONArray array2=new JSONArray();
jsonObj1=new JSONObject();
jsonObj2=new JSONObject();
array.put(new JSONObject().put("firstName", "John").put("lastName","Doe"))
.put(new JSONObject().put("firstName", "Anna").put("v", "Smith"))
.put(new JSONObject().put("firstName", "Peter").put("v", "Jones"));
array2.put(new JSONObject().put("firstName", "John").put("lastName","Doe"))
.put(new JSONObject().put("firstName", "Anna").put("v", "Smith"))
.put(new JSONObject().put("firstName", "Peter").put("v", "Jones"));
jsonObj1.put("employees", array);
jsonObj1.put("manager", array2);
Response response = null;
response = Response.status(Status.OK).entity(jsonObj1.toString()).build();
return response;