解析JSON并在对象列表中进行转换

时间:2019-03-29 10:28:36

标签: java json spring-boot

我从API收到了json响应。示例响应是这样的

{
 "meta": {
   "code": "200"
  },
 "data": [
   {
    "Id": 44,
    "Name": "Malgudi ABC"
  },
  {
    "Id": 45,
    "Name": "Malgudi, DEF"
  }
]
}

我正在尝试从中创建对象列表,为此我编写的代码是

private static List<TPDetails> getListOfTpDetails(ResponseEntity<?> responseEntity){
       ObjectMapper objectMapper = new ObjectMapper();
        List<TPDetails> tpDetailsList = objectMapper.convertValue(responseEntity.getBody().getClass(), new TypeReference<TPDetails>(){});
        return tpDetailsList;
    }

TPDetails对象是这样的

public class TPDetails {
    int Id;
    String Name;
}

我使用的代码导致

java.lang.IllegalArgumentException: Unrecognized field "meta" (class com.sbo.abc.model.TPDetails), not marked as ignorable (2 known properties: "Id", "Name"])
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.sbo.abc.model.TPDetails["meta"])

我想在列表中转换上述JSON响应

List<TPDetails> abc = [
{"Id": 44, "Name": "Malgudi ABC"},
{"Id": 45,"Name": "Malgudi DEF"}
]

任何帮助都是非常可取的。非常感谢

3 个答案:

答案 0 :(得分:1)

再创建2个类,例如

public class Temp {
    Meta meta;
    List<TPDetails> data;
}

public class Meta {
    String code;
}

现在将这个json转换为Temp类。

Temp temp = objectMapper.convertValue(responseEntity.getBody().getClass(), new TypeReference<Temp>(){});

已更新:

确保responseEntity.getBody()返回您上面提到的确切的Json字符串。

Temp temp = objectMapper.readValue(responseEntity.getBody(), new TypeReference<Temp>(){});

答案 1 :(得分:0)

java类的格式不反映您正在解析的json。我认为应该是:

class Response {
    Meta meta;
    List<TPDetails> data;
}

class Meta {
    String code;
}

然后您应将Response传递到您的TypeReferencenew TypeReference<Response>(){}

如果您不关心meta字段,则可以添加@JsonIgnoreProperties  进入您的响应类,并摆脱Meta类和字段。

答案 2 :(得分:0)

创建/更新以下类,由于没有服务,因此我正在存储JSON文件,但是应该可以解析并从以下模型中读取它。

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "meta",
        "data"
})
public class OuterPoJo {

    @JsonProperty("meta")
    private Meta meta;
    @JsonProperty("data")
    private List<TPDetails> data = null;

    @JsonProperty("meta")
    public Meta getMeta() {
        return meta;
    }

    @JsonProperty("meta")
    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    @JsonProperty("data")
    public List<TPDetails> getData() {
        return data;
    }

    @JsonProperty("data")
    public void setData(List<TPDetails> data) {
        this.data = data;
    }
}

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "code"
})
public class Meta {

    @JsonProperty("code")
    private String code;

    @JsonProperty("code")
    public String getCode() {
        return code;
    }

    @JsonProperty("code")
    public void setCode(String code) {
        this.code = code;
    }

}

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "Id",
        "Name"
})
public class TPDetails {

    @JsonProperty("Id")
    private Integer id;
    @JsonProperty("Name")
    private String name;

    @JsonProperty("Id")
    public Integer getId() {
        return id;
    }

    @JsonProperty("Id")
    public void setId(Integer id) {
        this.id = id;
    }

    @JsonProperty("Name")
    public String getName() {
        return name;
    }

    @JsonProperty("Name")
    public void setName(String name) {
        this.name = name;
    }

}


import java.io.File;
public class App {
public static void main(String[] args) throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    OuterPoJo myPoJo = objectMapper.readValue(
            new File("file.json"),
            OuterPoJo.class);

    for (TPDetails item : myPoJo.getData()) {
        System.out.println(item.getId() + ":" + item.getName());
    }

}
}

输出:

44:Malgudi ABC
45:Malgudi, DEF