java 嵌套的Json字符串到Pojo的Map(我可以将简单的JsonMap到Pojo,但无法Map嵌套的Json)

zrfyljdw  于 2022-12-10  发布在  Java
关注(0)|答案(1)|浏览(157)

{"fullname": "Darla Kertzmann","address": {"street": "4519 Mraz Point","city": { "cityName":"Melbourne","cityCode":"0000"}}}
这是一个我正在使用的模拟json。这个json字符串存储在我的cassandra数据库中。我可以Map简单的json,比如
{"name":"Dan","pass":"123" }
这些是
第一个
请让我知道我哪里错了我没有得到任何错误。只是值返回空。尝试了JSONObject并减少一个嵌套循环,尝试了Gson

z31licg0

z31licg01#

什么是//fullname mock here
其工作

package com.example.springdatademo;

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

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import lombok.Data;

@SpringBootTest
public class JsonTest {

  @Test
  void readValue() throws JsonProcessingException {
    ObjectMapper objMap = new ObjectMapper();
    Response resp = objMap.readValue("{\"fullname\": \"Darla Kertzmann\",\"address\": {\"street\": \"4519 Mraz Point\",\"city\": {                    \"cityName\":\"Melbourne\",\"cityCode\":\"0000\"}}}", Response.class);
    System.out.println(resp);
  }
}

@Data
class Response {
  //fullname mock here
  @JsonProperty("fullname")
  private String fullname;

  @JsonProperty("address")
  private Address address;
}

@Data
class Address {
  @JsonProperty("street")
  private String street;

  @JsonProperty("city")
  private City city;
}

@Data
class City {

  @JsonProperty("cityName")
  private String cityName;

  @JsonProperty("cityCode")
  private String cityCode;
}

相关问题