0

How to convert this JSON to an object?

enter image description here

Genesis
  • 358
  • 2
  • 8
Hà Pun
  • 1
  • 1
  • Would you please provide the code that you have tried? – Faysal Ahmed Mar 19 '20 at 04:14
  • search "json to java object" on the internet. I have personally used "http://www.jsonschema2pojo.org/" this site. If you are using a Gson converter tick that option, change the package name, choose a file name, check source type->JSON. Thats it then download the files and use – Nandu Dharmapalan Mar 19 '20 at 04:14
  • Please include what you started so far so that we can check. You may start to look for Gson on how to use it and check Gson Serializers. – Lester L. Mar 19 '20 at 04:26
  • Please follow the below link: https://stackoverflow.com/questions/6079505/converting-json-string-to-java-object – Apps Maven Mar 19 '20 at 05:55

3 Answers3

4

You can use Gson,

Gson a Java serialization/deserialization library to convert Java Objects into JSON and back

First u should put this code lines on gradle

dependencies {
  implementation 'com.google.code.gson:gson:2.8.6'
 }

If you use maven add as below

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.6</version>
 </dependency>

And finally after take json string,

String json = "here your json string";

Gson gson = new Gson();

YOURCLASS yourclass = gson.fromJson(json, YOURCLASS.class);

Another using examples here

errorau
  • 2,121
  • 1
  • 14
  • 38
1

Avoid using GSON trying to extract value for each JSON key and use the below "smart" method of automatic JSON to Java object generator.

What you exactly require is POJO. Additionally, you wouldn't want to write down the java program for a huge JSON object, do you?

The site to convert JSON string to java data class: jsonschema2pojo

Tutorial to convert JSON to Java Data class using the above site: Jackson 2 – Convert Java Object to / from JSON

As an example my JSON is:

{
   "name":"John",
   "age":30,
   "cars":[ [1, "Ford"], [2, "BMW"], [3, "Fiat"] ]
}

The generated "data" class for the above JSON is:

package com.example;

import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "name",
    "age",
    "cars"
})

public class Example {

    @JsonProperty("name")
    public String name;

    @JsonProperty("age")
    public int age;

    @JsonProperty("cars")
    public List<List<Integer>> cars = null;

}

And then use the above class:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/* .... */

ObjectMapper mapper = new ObjectMapper();

Example example;

try {
    example = mapper.readValue(jsonString.toString(), Example.class);
} catch (JsonProcessingException e) {
    e.printStackTrace();
}

// To get name
System.out.println(example.name);

Include jackson in gradle:

compile 'com.fasterxml.jackson.core:jackson-databind:2.8.5'
compile 'com.fasterxml.jackson.core:jackson-core:2.8.5'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.8.5'

If you are facing issues exclude META-INF/LICENSE in the android tag in the gradle file to avoid dup license file error in Android studio:

packagingOptions {
    exclude 'META-INF/LICENSE'
}

Just be sure to use proper configuration for the jsonschema2pojo website, my configurations are:

Configuration screenshot

halfer
  • 19,824
  • 17
  • 99
  • 186
FutureJJ
  • 2,368
  • 1
  • 19
  • 30
0

In addition to Gson library (@errorau answer), you can use java JSONObject class.

String jsonstr = "{\"age\":18, \"name\": \"Mr. Bean\", \"data\":[\"A\",\"B\"]}"
JSONObject json = new JSONObject(jsonstr);

To get values from JSONObject, use function getString, getInt, getJSONArray etc.

String name = json.getString("name");  // output: Mr. Bean
int age = json.getInt("age");  // output: 18

JSONArray stringArray = json.getJSONArray("data");
for(int i = 0; i < stringArray.size(); i++)
    System.out.println(stringArray.getString(i));  // output: A, B

or if you have nested json. Use getJSONObject function

String jsonstr = "{\"data\":{\"name\": \"Mr. Bean\"}}"
JSONObject json = new JSONObject(jsonstr);
JSONObject data = json.getJSONObject("data");
String name = data.getString("name")  // output: Mr. Bean