我想使用字符串创建一个 JSON 对象。
示例:JSON {"test1":"value1","test2":{"id":0,"name":"testName"}}
为了创建上面的 JSON,我正在使用它。
String message;
JSONObject json = new JSONObject();
json.put("test1", "value1");
JSONObject jsonObj = new JSONObject();
jsonObj.put("id", 0);
jsonObj.put("name", "testName");
json.put("test2", jsonObj);
message = json.toString();
System.out.println(message);
我想知道如何创建一个包含 JSON 数组的 JSON。
下面是示例 JSON。
{
"name": "student",
"stu": {
"id": 0,
"batch": "batch@"
},
"course": [
{
"information": "test",
"id": "3",
"name": "course1"
}
],
"studentAddress": [
{
"additionalinfo": "test info",
"Address": [
{
"H.No": "1243",
"Name": "Temp Address",
"locality": "Temp locality",
"id":33
},
{
"H.No": "1243",
"Name": "Temp Address",
"locality": "Temp locality",
"id":33
},
{
"H.No": "1243",
"Name": "Temp Address",
"locality": "Temp locality",
"id":36
}
],
"verified": true,
}
]
}
谢谢。
原文由 ravi 发布,翻译遵循 CC BY-SA 4.0 许可协议
org.json.JSONArray
可能是你想要的。