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.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Mohamad Mousheimish
  • 1,641
  • 3
  • 16
  • 48
  • What about this? https://stackoverflow.com/questions/44295665/how-to-create-a-jsonobject-from-string-in-kotlin/44297201 – OhhhThatVarun Feb 03 '20 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)` – omz1990 Feb 03 '20 at 11:18
  • @VarunRaj yes I've checked it and it didn't work for me – Mohamad Mousheimish Feb 03 '20 at 11:25
  • I think you should use https://stackoverflow.com/a/47283450/7436566 this way to create a JSON object as you already know the JSON keys. – OhhhThatVarun Feb 03 '20 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 – Mohamad Mousheimish Feb 03 '20 at 11:28
  • @VarunRaj I've also tried it, I don't know why but creating an Empty `JSONObject` threw the same error!! – Mohamad Mousheimish Feb 03 '20 at 11:29
  • Can you show full java stacktrace of error? Or is it compiler error? – xinaiz Feb 03 '20 at 11:30
  • How can it throw the same error? The error you are getting is `Cannot evaluate org.json.JSONObject.toString()` and there is no `toString()` involved in that answer. – OhhhThatVarun Feb 03 '20 at 11:30
  • Are you using some custom font in your IDE? I know something similar has happened to a colleague of mine (not in this context), but had to do with having a string in a file with a custom font. Also have seen issues with copy pasting some illegal characters. But yes, you’re right, there must be some difference as it ran on my machine – omz1990 Feb 03 '20 at 11:36
  • @VarunRaj see the update, it deos! – Mohamad Mousheimish Feb 03 '20 at 11:36
  • @MohamadMousheimish hmm very strange. – OhhhThatVarun Feb 03 '20 at 11:39
  • @xinaiz I've checked the full stack trace the error provided was `Method toString in org.json.JSONObject not mocked.` I've added an Update after googling this error. Please check it out – Mohamad Mousheimish Feb 03 '20 at 11:47
  • Yes, check the update. it's now returning null instead of throwing an execption. Even though my json string is correct – Mohamad Mousheimish Feb 03 '20 at 11:50
  • The `toString` method in `JSONObject` is defined as `try { return this.toString(0); } catch (Exception e) { return null; }`. Please put breakpoint at the `return null` statement and check what exception cased it. – xinaiz Feb 03 '20 at 11:52

1 Answers1

3

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

Mohamad Mousheimish
  • 1,641
  • 3
  • 16
  • 48