2017-01-23 150 views
1

我無法閱讀這JSON,代碼似乎工作一個JSON,但有2個問題閱讀使用傑克遜

  1. 只讀取JSON的一個塊,不完全。
  2. 它始終具有「null」作爲屬性中的值。

我一直在試圖顯示json組織在控制檯,但是當我嘗試這2件事情發生。

樣品的JSON數據:

{ 
    "RestResponse" : { 
    "messages" : [ "More webservices are available at http://www.groupkt.com/post/f2129b88/services.htm", "Total [249] records found." ], 
    "result" : [ { 
     "name" : "Afghanistan", 
     "alpha2_code" : "AF", 
     "alpha3_code" : "AFG" 
    }, { 
     "name" : "Åland Islands", 
     "alpha2_code" : "AX", 
     "alpha3_code" : "ALA" 
    }, { 
     "name" : "Albania", 
     "alpha2_code" : "AL", 
     "alpha3_code" : "ALB" 
    }, ... 
    ] 
    } 
} 

我的代碼:

public class jsonController { 

public void run() { 
    ObjectMapper mapper = new ObjectMapper(); 
    try { 

     jsonHandler obj = mapper.readValue(new URL("http://services.groupkt.com/country/get/all"), jsonHandler.class); 
     //Organized Print 
     String organizedprint = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj); 
     System.out.println(organizedprint); 


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

,並在主我有

jsonController obj = new jsonController(); 
    obj.run(); 

而這裏的jsonHandler

@JsonIgnoreProperties(ignoreUnknown=true) 
public class jsonHandler { 
    private String restResponse; 
    private String messages; 
    private String result; 
    private String name; 
    private String alpha2; 
    private String alpha3; 
} 

任何想法我做錯了什麼?

+0

什麼是jsonHandler?它是你的映射類嗎?你可以發佈嗎? –

+0

@fabienbk在那裏,它有getter和setters,我沒有把它們放在這裏使它更短。 – JuanPa

+0

謝謝。請參閱下面的回覆。 –

回答

1

您在模型中錯誤地聲明瞭您的數據類型。您的Java代碼聲明數據將具有包含6個字符串屬性的單個對象。服務器提供的JSON數據完全不是這樣的。例如,messages是一個字符串列表,而result是一個對象列表,而不是一個字符串。您需要相應地聲明您的Java模型。

例如:

public class jsonHandler 
    { 
    private RestResponseStructure restResponse; 
    } 

public class RestResponseStructure 
    { 
    private List<String> messages; 
    private List<CountryRecord> results; 
    } 

public class CountryRecord { 
    private String name; 
    private String alpha2_code; 
    private String alpha3_code; 
} 
0

好您的映射類,jsonHandler是錯誤的。首先,它應該正確地大寫(JsonHandler

使用http://www.jsonschema2pojo.org/我生成了一個更好的模型。它由3個類組成。只需將包「com.example」更改爲您的包即可。

package com.example; 

import java.util.HashMap; 
import java.util.Map; 
import com.fasterxml.jackson.annotation.JsonAnyGetter; 
import com.fasterxml.jackson.annotation.JsonAnySetter; 
import com.fasterxml.jackson.annotation.JsonIgnore; 
import com.fasterxml.jackson.annotation.JsonInclude; 
import com.fasterxml.jackson.annotation.JsonProperty; 
import com.fasterxml.jackson.annotation.JsonPropertyOrder; 

@JsonInclude(JsonInclude.Include.NON_NULL) 
@JsonPropertyOrder({ 
"RestResponse" 
}) 
public class JsonHandler { 

@JsonProperty("RestResponse") 
private RestResponse restResponse; 
@JsonIgnore 
private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

@JsonProperty("RestResponse") 
public RestResponse getRestResponse() { 
return restResponse; 
} 

@JsonProperty("RestResponse") 
public void setRestResponse(RestResponse restResponse) { 
this.restResponse = restResponse; 
} 

@JsonAnyGetter 
public Map<String, Object> getAdditionalProperties() { 
return this.additionalProperties; 
} 

@JsonAnySetter 
public void setAdditionalProperty(String name, Object value) { 
this.additionalProperties.put(name, value); 
} 

} 

com.example.RestResponse.java

package com.example; 

import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import com.fasterxml.jackson.annotation.JsonAnyGetter; 
import com.fasterxml.jackson.annotation.JsonAnySetter; 
import com.fasterxml.jackson.annotation.JsonIgnore; 
import com.fasterxml.jackson.annotation.JsonInclude; 
import com.fasterxml.jackson.annotation.JsonProperty; 
import com.fasterxml.jackson.annotation.JsonPropertyOrder; 

@JsonInclude(JsonInclude.Include.NON_NULL) 
@JsonPropertyOrder({ 
"messages", 
"result" 
}) 
public class RestResponse { 

@JsonProperty("messages") 
private List<String> messages = null; 
@JsonProperty("result") 
private List<Result> result = null; 
@JsonIgnore 
private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

@JsonProperty("messages") 
public List<String> getMessages() { 
return messages; 
} 

@JsonProperty("messages") 
public void setMessages(List<String> messages) { 
this.messages = messages; 
} 

@JsonProperty("result") 
public List<Result> getResult() { 
return result; 
} 

@JsonProperty("result") 
public void setResult(List<Result> result) { 
this.result = result; 
} 

@JsonAnyGetter 
public Map<String, Object> getAdditionalProperties() { 
return this.additionalProperties; 
} 

@JsonAnySetter 
public void setAdditionalProperty(String name, Object value) { 
this.additionalProperties.put(name, value); 
} 

} 

com.example.Result.java

package com.example; 

import java.util.HashMap; 
import java.util.Map; 
import com.fasterxml.jackson.annotation.JsonAnyGetter; 
import com.fasterxml.jackson.annotation.JsonAnySetter; 
import com.fasterxml.jackson.annotation.JsonIgnore; 
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", 
"alpha2_code", 
"alpha3_code" 
}) 
public class Result { 

@JsonProperty("name") 
private String name; 
@JsonProperty("alpha2_code") 
private String alpha2Code; 
@JsonProperty("alpha3_code") 
private String alpha3Code; 
@JsonIgnore 
private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

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

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

@JsonProperty("alpha2_code") 
public String getAlpha2Code() { 
return alpha2Code; 
} 

@JsonProperty("alpha2_code") 
public void setAlpha2Code(String alpha2Code) { 
this.alpha2Code = alpha2Code; 
} 

@JsonProperty("alpha3_code") 
public String getAlpha3Code() { 
return alpha3Code; 
} 

@JsonProperty("alpha3_code") 
public void setAlpha3Code(String alpha3Code) { 
this.alpha3Code = alpha3Code; 
} 

@JsonAnyGetter 
public Map<String, Object> getAdditionalProperties() { 
return this.additionalProperties; 
} 

@JsonAnySetter 
public void setAdditionalProperty(String name, Object value) { 
this.additionalProperties.put(name, value); 
} 

} 
+0

我認爲有用但信息太多。無論如何謝謝回答。 – JuanPa