0

I know that this question is asked hundreds of time, I've checked more than 30 answer and none worked for me.

I'm running a unit test in android studio using Kotlin. I'm trying to build a JSONObject using a String.

The method I'm using to build a simple json object is always throwing Cannot evaluate org.json.JSONObject.toString().

The string I'm using definitely have a valid Json format.

Is this a bug in android or what??

Here's the method I'm using:

fun createTaskJson(): JSONObject {
    val jsonString =
        "{\"id\":138,\"title\":\"G0102-025\",\"type\":\"New Land Evaluation\",\"taskCreationDate\":\"28/01/2020\",\"taskDeliveryDate\":\"02/02/2020\",\"taskCreationTime\":\"05:15 AM\"}"
    val realJson = JSONObject(jsonString)
    return realJson
}

UPDATE 1: Please note that also creating an empty JSONObject will throw the same error !!

enter image description here

UPDATE 2:

After checking the stacktrace, I've found the following error:

Method toString in org.json.JSONObject not mocked.

I've googled it and the solution was to add the following configuration into my build.gradle file:

    testOptions {
    unitTests.returnDefaultValues = true
}

But adding this made the returned JSONObject null instead of throwing an exception, even though my string is formatted correctly.

14
  • What about this? stackoverflow.com/questions/44295665/… Commented Feb 3, 2020 at 11:18
  • Your code works fine for me as it is, so I don't see any issue in the string. How about just try out Gson to see if there is an error there too. It also works fine when I do: Gson().fromJson("your string", HashMap::class.java) Commented Feb 3, 2020 at 11:18
  • @VarunRaj yes I've checked it and it didn't work for me Commented Feb 3, 2020 at 11:25
  • I think you should use stackoverflow.com/a/47283450/7436566 this way to create a JSON object as you already know the JSON keys. Commented Feb 3, 2020 at 11:26
  • @omz1990 yes the Gson method you've mentioned worked. And returned a hash map with the value inside of it. But if you said that no problem happened with the same code at your machine, then there's something wrong with my project/configuration or something like that Commented Feb 3, 2020 at 11:28

1 Answer 1

4

After the updates mentioned in the question above. And after lots of searching. I found out the following:

The solution was in adding the following line into build.gradle file:

implementation group: 'org.json', name: 'json', version: '20190722'

I don't know why, but it's like android have an object with names JSONArray and JSONObject but when adding the implementation to the build.gradlefile. It handles JSONObject and JSONArray in another way.

Because after adding the statement to the gradle file, the following warning will be giving by android:

json define classes that conflict with classes now provided by Android

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.