简体   繁体   中英

How to convert this JSON to object in JAVA android?

How to convert this JSON to an object?

在此处输入图片说明

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

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:

配置截图

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM