简体   繁体   中英

Create JSONObject with org.json lib in java

I try to create JSONObject with JSONArray in Java with org.json lib. I write this JSON string:

"{\\"header\\", \\"array\\":[{\\"b\\", \\"a\\", \\"c\\"}]}"

But if I try generate JSONObject like here:

JSONObject json = new JSONObject(jsonString);

Then finally I see this string:

"{\\"header\\", \\"array\\":[{\\"a\\", \\"b\\", \\"c\\"}]}"

But I need see string "{\\"header\\", \\"array\\":[{\\"b\\", \\"a\\", \\"c\\"}]}" , cause this structure of elements more important. How can I correctly create JSONObject from string without issues? I'm new in JSON

Curly brackets ( {} ) denote an object, and objects are unordered. Therefore, the two representations are equivalent. If the order of the elements is important, you should use an array ( [] ) instead of an object: {"header", "array": ["b", "a", "c"]} .

(See RFC 7159, Section 1 for an overview of the JSON data types.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM