简体   繁体   中英

JSONObject[str] is not a JSONObject while parsing JSON

I am trying to deserialize (parse) this JSON: {"test": "TEST2"}

I am new to Java, so I've searched a lot on Google and I finally found the answer here: https://stackoverflow.com/a/18998203/8524395

So, I am trying to deserialize my JSON like that:

JSONObject obj = new JSONObject("{\"test\": \"TEST2\"}");
String testValue = obj.getJSONObject("test").toString();

But I am getting the error: JSONObject obj = new JSONObject("{\\"test\\": \\"TEST2\\"}"); String pageName = obj.getJSONObject("test").toString(); JSONObject obj = new JSONObject("{\\"test\\": \\"TEST2\\"}"); String pageName = obj.getJSONObject("test").toString();

Any help would be appreciated.

Thanks!

You should get a string not another object:

String testValue = obj.getString("test");

In other word, test is a key that maps to a string of value TEST2 .

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