How to create JSON Object using String?

How to create JSON Object using String?



I want to create a JSON Object using String.



Example :
JSON "test1":"value1","test2":"id":0,"name":"testName"


"test1":"value1","test2":"id":0,"name":"testName"



In order to create the above JSON I am using this.


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



I want to know how can I create a JSON which has JSON Array in it.



Below is the sample 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,

]



Thanks.




2 Answers
2



JSONArray may be what you want.


JSONArray


String message;
JSONObject json = new JSONObject();
json.put("name", "student");

JSONArray array = new JSONArray();
JSONObject item = new JSONObject();
item.put("information", "test");
item.put("id", 3);
item.put("name", "course1");
array.add(item);

json.put("course", array);

message = json.toString();

// message
// "course":["id":3,"information":"test","name":"course1"],"name":"student"





How do I convert it back to a JSONObject from a string?
– morha13
Feb 23 at 17:42


JSONObject





JSONObject jsonObj = new JSONObject("your_json_string");
– Camille
Feb 28 at 7:44



JSONObject jsonObj = new JSONObject("your_json_string");





FYI that this will fail to parse JSON arrays (even though they are technically valid JSON). For example, trying JSONObject("["foo":2, "bar": 3]"); results in A JSONObject text must begin with '{' at 1 [character 2 line 1]
– user2490003
Aug 21 at 5:44


JSONObject("["foo":2, "bar": 3]");


A JSONObject text must begin with '{' at 1 [character 2 line 1]



In contrast to what the accepted answer proposes, the documentation says that for JSONArray() you must use put(value) no add(value).


put(value)


add(value)



https://developer.android.com/reference/org/json/JSONArray.html#put(java.lang.Object)



(Android API 19-27. Kotlin 1.2.50)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế