Reputation: 1180
I've written a Java Method for an Android Studio (3.0.1) project that creates a JSONObject from a String. Now I've written a JUnit test to test that the JSON object is correct.
The problem is always that the JSONObject is NULL.
For example, I tried running the following code:
String response = "{\"songs\":\"title\"}";
System.out.println(response);
JSONObject submitPlaylistResponse = new JSONObject(response);
System.out.println(submitPlaylistResponse.toString());
The output is:
{"songs":"title"}
null
Why is my JsonObject still null? What am I missing? I feel like I'm missing something trivial/simple.
Upvotes: 1
Views: 2713
Reputation: 46
After viewing JSONObject code I found this:
public String toString() {
return "null";
}
As you see, method always returns "null" string not null object. In this case, if the task is to check the correctness of your JSONObject you can follow @G.Dator answer or use String toString (int indentSpaces).
See also.
Upvotes: 3
Reputation: 71
Add below line to build.gradle dependencies
testImplementation 'org.json:json:20190722'
Refer this question for more details.
Upvotes: 1
Reputation: 8471
Probably you are using wrong library. Check your import. It must be like this. org.json.JSONObject; I've checked your code and it's working on my android studio 2.3
Upvotes: 0