-1

I'm new in Java and I'm creating a web app using Servlet in Eclipse. I want to convert string to JSON using this code :

import org.json.JSONException; 
import org.json.JSONObject;

JSONObject jsonObject = null;
jsonObject = new JSONObject(STRING);

System.out.println(jsonObject.getString("PROPERTY_NAME"));

It works fine if STRING be equal to "{'status':0}" and jsonObject.getString("status") gives me 0.

But I get a response from API like "{"status":0}" and jsonObject.getString("status") gives me error because jsonObject is :

{}

And the error is :

org.json.JSONException: JSONObject["status"] not found.

Do you have any solution about this?

Bahman Parsa Manesh
  • 2,314
  • 3
  • 16
  • 32
  • 2
    which lib are you using? That example you have given - that _{"status":0}_ - is a valid JSON string. – KarelG Oct 09 '19 at 08:25
  • I'm using `org.json.JSONException` and `org.json.JSONObject` – Bahman Parsa Manesh Oct 09 '19 at 08:28
  • 1
    That library is used by a lot and does the job. (there is a `jsonObject.has()`) check if you want to check its keys safely. See if the "STRING" is actually that string you have passed on. – KarelG Oct 09 '19 at 08:30
  • actually my response is too long and status is just one part of it. i don't know but maybe there is an issue in response structure. – Bahman Parsa Manesh Oct 09 '19 at 08:37
  • 1
    well, there are online JSON string validators. Maybe you can use one of these to see if it is valid. But that library throws a JSONException if the string is not a valid JSON String. It is best to debug it. You said that using `jsonObject.getString("status")` gives an error. Can you post it? It might be that yours `status` is just on another place in the structure. – KarelG Oct 09 '19 at 08:43
  • error added to question – Bahman Parsa Manesh Oct 09 '19 at 08:49
  • Possible duplicate of [The difference between getString() and optString() in Json](https://stackoverflow.com/questions/13790726/the-difference-between-getstring-and-optstring-in-json) –  Oct 09 '19 at 11:29

3 Answers3

2

Problem is with the value not key. i've tested this, it works

JSONObject jsonObject = null;
jsonObject = new JSONObject("{\"status\":0}");
System.out.println(jsonObject.getInt("status"));

or this

 JSONObject jsonObject = null;
 jsonObject = new JSONObject("{\"status\":'0'}");
 System.out.println(jsonObject.getString("status"));
Hadi Moloodi
  • 639
  • 7
  • 12
  • 1
    fyi, it does not matter. If the value is an number, `getString` will give a String representation of it. If you don't believe me, check its source code: [JSONObject.getString](https://android.googlesource.com/platform/dalvik/+/f2f7880a40aed1c8d9c27db49226e47396556aac/libcore/json/src/main/java/org/json/JSONObject.java#511) then [this](https://android.googlesource.com/platform/dalvik/+/f2f7880a40aed1c8d9c27db49226e47396556aac/libcore/json/src/main/java/org/json/JSON.java#82) which does a `String.valueOf()` – KarelG Oct 09 '19 at 08:41
  • 1
    @KarelG You're assuming that OP uses Android and not for example this here: https://mvnrepository.com/artifact/org.json/json. That lib does fail on `0`, not on `'0'`. I currently see no hint what OP really uses, but since there is TomCat in the Stacktrace, I doubt it is Android. – Tom Oct 09 '19 at 09:45
  • I'm creating a web application (servlet) in eclipse. – Bahman Parsa Manesh Oct 09 '19 at 11:14
  • you can use the approach I mentioned in my answer it will work. I used this mvnrepository.com/artifact/org.json/json as my lib. – Hadi Moloodi Oct 09 '19 at 11:47
  • Actually problem was not because of value. both of `getInt` and `getString` works fine for `"{\"status\":0}"`. My problem was changing `"{"status":0}"` to `"{\"status\":0}")` correctly. – Bahman Parsa Manesh Oct 09 '19 at 12:11
1

You need to escape your double quotes in your STRING variable:

"{\"status\":0}"

You can do that programmatically like that (we need to call toString() because STRING is an instance of StringBuilder):

String escapedJsonStr = STRING.toString().replaceAll("\"", "\\\"");
Vaidas
  • 1,494
  • 14
  • 21
  • Thanks for answer. `STRING` is `StrngBuilder` and I give this error on replaceAll : `The method replaceAll(String, String) is undefined for the type StringBuilder` – Bahman Parsa Manesh Oct 09 '19 at 08:44
0

I can help you with below example...

for (String key: jsonObject.keySet()){ System.out.println(key); }

This will fetch you the set of Keys in the JSON.

JSONObject json_array = args.optJSONObject(0);

Iterator keys = json_array.keys();

while( keys.hasNext() ) { String key = (String) keys.next(); System.out.println("Key: " + key); System.out.println("Value: " + json_array.get(key)); }

I recommend following the link for a thorough understanding of Java and JSON -- Example

rkoots
  • 199
  • 5
  • 4