user10037542
user10037542

Reputation:

Java, map a json

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.

Upvotes: 1

Views: 123

Answers (1)

Paul
Paul

Reputation: 20061

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

Upvotes: 2

Related Questions