简体   繁体   中英

Why JSONObject converts stringified array to JSONArray on put operation? How to stop this?

  • When I tried to put stringified array in JSONObject, it became a JSONArray. But I want to keep the stringified array. Any help will be appreciated.

eg:

JSONObject jsonObj = new JSONOject();

jsonObj.put("stringified_array", "[1,2]");

So, when I debugged it, the jsonObj properties were

key : "stringified_array", value : [1,2] (JSONArray)

Edit: Here is the link for sample code: https://onlinegdb.com/UbV6VXvpJ . Code won't compile due to missing packages in online ide, just for better understanding of scenario I have added the link.

Here is why, if you look at the declaraction of constructor of JSONObject , you will find what you are looking for. One of the constructor of JSONObject class looks like following:

public org.json.JSONObject put(java.lang.String key, java.util.Map<?,?> value) throws org.json.JSONException { /* compiled code */ }

If you know how to do reverse engineering , you should be able to find more about the actual implementation:)!

Hopefully, this will help you out!

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