1

Here's what I wanna do
I have a json, like:

{ "demoNumber":123, "demoText":"asdasdasd" } 

and I wanna make a simple String array from it, which should be

["demoNumber","demoText"] 

In the app we're making the user can add any type of data, so we can't do data models for everything, that's not an option

I have added json to my Gradle:

dependencies { implementation 'org.json:json:20180130' } 

But it still can't find the method.

2
  • So what did you try..? Commented Feb 23, 2019 at 16:20
  • Something like Paul answered, I just can't find that map method Commented Feb 23, 2019 at 16:35

1 Answer 1

2

Assuming you have the JSON as a string, this example uses the JSON-java library:

JSONObject jo = new JSONObject(myJsonStr); Set<String> keys = jo.toMap().keySet(); // You should be able to extract an array from the set of keys 

See also https://www.baeldung.com/java-org-json

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

8 Comments

Maybe i ducked up something but I get cannot resolve method toMap
update, added org.json to gradle, and it still cant find this method
That's odd because the method is in the source code (last method in class): github.com/stleary/JSON-java/blob/master/JSONObject.java We use the library in our project and I can confirm the method is there. Be sure you're using version 20180813 or 20180130, I know for sure the method is in both of those versions.
Yeah, I checked the code and found the method too, but android studio somewhy cannot find it. The only thing I have to do is import it in my gradle right?
Yes. Check the version you're using by running gradle -q dependencies --configuration compile. You can leave off --configuration compile to see the dependencies for all configurations. docs.gradle.org/current/userguide/inspecting_dependencies.html
|