Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用jackson API将json字符串映射到java对象_Java_Json_Web Services_Rest_Jackson - Fatal编程技术网

无法使用jackson API将json字符串映射到java对象

无法使用jackson API将json字符串映射到java对象,java,json,web-services,rest,jackson,Java,Json,Web Services,Rest,Jackson,我正在编写一个web服务(服务器+客户端)。我能够创建一个服务,它会返回以下json { "cities": { "city": [ { "name": "New Delhi", "population": "19M", "telephonecode": "011" }, { "name": "Mumbai", "popu

我正在编写一个web服务(服务器+客户端)。我能够创建一个服务,它会返回以下json

{
"cities": {
    "city": [
        {
            "name": "New Delhi",
            "population": "19M",
            "telephonecode": "011"
        },
        {
            "name": "Mumbai",
            "population": "21M",
            "telephonecode": "022"
        },
        {
            "name": "Chennai",
            "population": "10M",
            "telephonecode": "044"
        }
    ]
}
}

我的POJO是

@XmlRootElement(name = "cities")
public class RestFulCities {

List<RestFulCity> restFulCityList;

@XmlElement(name = "city")
public List<RestFulCity> getRestFulCityList() {
    return restFulCityList;
}

public void setRestFulCityList(List<RestFulCity> restFulCityList) {
    this.restFulCityList = restFulCityList;
}
}

@XmlRootElement(name = "city")
public class RestFulCity {
private String name;
private String telephonecode;
private String population;

public RestFulCity(String name, String telephonecode, String population) {
    this.name = name;
    this.telephonecode = telephonecode;
    this.population = population;
}

public RestFulCity(City city) {
    this.name = city.getName();
    this.telephonecode = city.getTelephonecode();
    this.population = city.getPopulation();
}
@XmlElement
public String getName() {
    return name;
}
@XmlElement
public String getTelephonecode() {
    return telephonecode;
}

@XmlElement
public String getPopulation() {
    return population;
}
}
但问题是:我得到了以下例外

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "cities"(Class com.techartifact.example.spring.model.RestFulCities), not marked as ignorable
 at [Source: java.io.StringReader@1d35bf2; line: 1, column: 12] (through reference    chain: com.techartifact.example.spring.model.RestFulCities["cities"])
当我使用以下属性时:

@JsonIgnoreProperties(ignoreUnknown = true)
虽然我没有得到异常,但是我的restFulCityList为null,这是不需要的


请提供帮助

您正在使用JAXB注释,因此需要使用正确的模块正确配置
ObjectMapper
;你需要这个项目。使用您喜爱的依赖关系管理系统添加它,并按如下方式使用:

JaxbAnnotationModule module = new JaxbAnnotationModule();
// configure as necessary
objectMapper.registerModule(module);
注意:这是给Jackson 2.x的。默认情况下,Jackson1.x支持JAXB,但该版本不再受支持,并且考虑到您有这个问题,您可能正在使用Jackson2.x

更新:JAXB注释非常适合与XML系统进行互操作,但如果可以的话,您应该真正使用Jackson自己的注释。这样就不需要jaxb模块和
ObjectMapper
的配置。此外,Jackson中的某些功能只能通过其注释提供,因为在JAXB中没有等效功能。

找到了解决方案

使用以下代码:

 JSONObject primary_contact = new JSONObject(s);
 String s1 = primary_contact.getString("cities");
 JSONObject primary_contact1 = new JSONObject(s1);
 String s2 = primary_contact1.getString("city");


 List<City> citiesList = new ObjectMapper().readValue(s2, new TypeReference<List<City>>() { });
public class Client {

    static final String REST_URI = "http://localhost:8080/springrest/rest/";
    static final String CITIES = "cities";
    static final String CITIES_BHUVAN = "cities/bhuvan";
    static final String BHUVAN = "bhuvan";
    static final String BHUVAN_BHUVAN = "bhuvan/bhuvan";

    public static void main(String[] args) throws JSONException {

        String s = "";

        WebClient plainAddClient = WebClient.create(REST_URI);
        plainAddClient.path(CITIES).accept("application/json");
        s = plainAddClient.get(String.class);
        try {

            JSONObject primary_contact = new JSONObject(s);
            String s1 = primary_contact.getString("cities");
            JSONObject primary_contact1 = new JSONObject(s1);
            String s2 = primary_contact1.getString("city");


            List<City> citiesList = new ObjectMapper().readValue(s2, new TypeReference<List<City>>() { });

            for(City city : citiesList) {
                System.out.println("----------START---------");
                System.out.println(city.getName());
                System.out.println(city.getPopulation());
                System.out.println(city.getTelephonecode());
                System.out.println("---------END----------");
            }

        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        WebClient xmlAddClient = WebClient.create(REST_URI);
        xmlAddClient.path(CITIES_BHUVAN).accept("application/json");
        s = xmlAddClient.get(String.class);
        System.out.println(s);

        WebClient plainSubClient = WebClient.create(REST_URI);
        plainSubClient.path(BHUVAN).accept("application/json");
        s = plainSubClient.get(String.class);
        System.out.println(s);

        WebClient xmlSubClient = WebClient.create(REST_URI);
        xmlSubClient.path(BHUVAN_BHUVAN).accept("application/json");
        s = xmlSubClient.get(String.class);
        System.out.println(s);
    }
}
JSONObject primary\u contact=新的JSONObject;
字符串s1=主要联系人.getString(“城市”);
JSONObject primary_contact1=新JSONObject(s1);
字符串s2=主触点1.getString(“城市”);
List citiesList=new ObjectMapper().readValue(s2,new TypeReference(){});
客户端代码应为:

 JSONObject primary_contact = new JSONObject(s);
 String s1 = primary_contact.getString("cities");
 JSONObject primary_contact1 = new JSONObject(s1);
 String s2 = primary_contact1.getString("city");


 List<City> citiesList = new ObjectMapper().readValue(s2, new TypeReference<List<City>>() { });
public class Client {

    static final String REST_URI = "http://localhost:8080/springrest/rest/";
    static final String CITIES = "cities";
    static final String CITIES_BHUVAN = "cities/bhuvan";
    static final String BHUVAN = "bhuvan";
    static final String BHUVAN_BHUVAN = "bhuvan/bhuvan";

    public static void main(String[] args) throws JSONException {

        String s = "";

        WebClient plainAddClient = WebClient.create(REST_URI);
        plainAddClient.path(CITIES).accept("application/json");
        s = plainAddClient.get(String.class);
        try {

            JSONObject primary_contact = new JSONObject(s);
            String s1 = primary_contact.getString("cities");
            JSONObject primary_contact1 = new JSONObject(s1);
            String s2 = primary_contact1.getString("city");


            List<City> citiesList = new ObjectMapper().readValue(s2, new TypeReference<List<City>>() { });

            for(City city : citiesList) {
                System.out.println("----------START---------");
                System.out.println(city.getName());
                System.out.println(city.getPopulation());
                System.out.println(city.getTelephonecode());
                System.out.println("---------END----------");
            }

        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        WebClient xmlAddClient = WebClient.create(REST_URI);
        xmlAddClient.path(CITIES_BHUVAN).accept("application/json");
        s = xmlAddClient.get(String.class);
        System.out.println(s);

        WebClient plainSubClient = WebClient.create(REST_URI);
        plainSubClient.path(BHUVAN).accept("application/json");
        s = plainSubClient.get(String.class);
        System.out.println(s);

        WebClient xmlSubClient = WebClient.create(REST_URI);
        xmlSubClient.path(BHUVAN_BHUVAN).accept("application/json");
        s = xmlSubClient.get(String.class);
        System.out.println(s);
    }
}
公共类客户端{
静态最终字符串REST_URI=”http://localhost:8080/springrest/rest/";
静态最终字符串CITIES=“CITIES”;
静态最终字符串CITIES_BHUVAN=“CITIES/BHUVAN”;
静态最终字符串BHUVAN=“BHUVAN”;
静态最终字符串BHUVAN_BHUVAN=“BHUVAN/BHUVAN”;
公共静态void main(字符串[]args)抛出JSONException{
字符串s=“”;
WebClient plainAddClient=WebClient.create(REST\u URI);
plainAddClient.path(CITIES.accept(“application/json”);
s=plainAddClient.get(String.class);
试一试{
JSONObject primary_contact=新的JSONObject;
字符串s1=主要联系人.getString(“城市”);
JSONObject primary_contact1=新JSONObject(s1);
字符串s2=主触点1.getString(“城市”);
List citiesList=new ObjectMapper().readValue(s2,new TypeReference(){});
for(城市:citiesList){
System.out.println(“------------START----------”);
System.out.println(city.getName());
System.out.println(city.getPopulation());
System.out.println(city.getTelephonecode());
System.out.println(“------结束------------”;
}
}捕获(JSONParsee异常){
e、 printStackTrace();
}捕获(JsonMappingException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
WebClient xmlAddClient=WebClient.create(REST\u URI);
xmlAddClient.path(CITIES_BHUVAN).accept(“application/json”);
s=xmlAddClient.get(String.class);
系统输出打印项次;
WebClient printsubclient=WebClient.create(REST\u URI);
plainSubClient.path(BHUVAN.accept)(“应用程序/json”);
s=plainSubClient.get(String.class);
系统输出打印项次;
WebClient xmlSubClient=WebClient.create(REST\u URI);
xmlSubClient.path(BHUVAN_BHUVAN).accept(“application/json”);
s=xmlSubClient.get(String.class);
系统输出打印项次;
}
}

为什么有两次
@XmlRootElement(name=“city”)
?您还没有使用
@XmlRootElement(name=“cities”)
定义任何内容,我认为这应该是
列表