2017-02-13 73 views
0

我已使HttpsURLConnection接收有關我的服務器的一些信息。 反應的結果是:Jackson JSON無法構建「About」的實例:反序列化問題

{"about":{"title":"NiFi","version":"1.1.0","uri":"https://localhost:443/api/","contentViewerUrl":"/nifi-content-viewer/","timezone":"CET"}} 

如何可以提取所有屬性和鍵/值?

About.class文件

public class About { 

private List<AboutObject> about; 
public About() 
{ 
// this.about = about; 
} 
public List<AboutObject> getAbout() { 
    return this.about; 
} 
public void setAbout(List<AboutObject> about) { 
    this.about = about; 
} 
} 

AboutObject.class

public class AboutObject { 

private String title; 
private String uri; 
private String contentViewerUrl; 
private String timezone; 

public String getTitle() 
{ 
    return this.title; 
} 

public void setTitle(String title) 
{ 
    this.title = title; 
} 

public String getUri() 
{ 
    return this.uri; 
} 

public void setUri(String uri) 
{ 
    this.uri = uri; 
} 

public String getContentViewerUrl() 
{ 
    return this.contentViewerUrl; 
} 

public void setContentViewerUrl(String contentViewerUrl) 
{ 
    this.contentViewerUrl = contentViewerUrl; 
} 

public String getTimeZone() 
{ 
    return this.timezone; 
} 

public void setTimeZone(String timezone) 
{ 
    this.timezone = timezone; 
} 
} 

Main.class

HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); 

// optional default is GET 
con.setRequestMethod("GET"); 

//add request header 
con.setRequestProperty("User-Agent", USER_AGENT); 

int responseCode = con.getResponseCode(); 
System.out.println("\nSending 'GET' request to URL : " + url); 
System.out.println("Response Code : " + responseCode); 

BufferedReader in = new BufferedReader(
     new InputStreamReader(con.getInputStream())); 
String inputLine; 
StringBuffer response = new StringBuffer(); 

while ((inputLine = in.readLine()) != null) { 
    response.append(inputLine); 
} 
in.close(); 

//print result 
System.out.println(response.toString()); 

System.out.println("Contenu de in = " + in.toString()); 

ObjectMapper mapper = new ObjectMapper(); 
//Staff objStaff = new Staff(); 

System.out.println("Object to JSON in file"); 
mapper.writeValue(new File("output/file.json"), response); 
System.out.println("Convert JSON string from file to Object"); 
//String about = mapper.readValue(new File("output/file.json"), String.class); 
About about = mapper.readValue(new File("output/file.json"), About.class); 

錯誤

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of About: no String-argument constructor/factory method to deserialize from String value ('{"about":{"title":"NiFi","version":"1.1.0","uri":"https://localhost:443/api/","contentViewerUrl":"/nifi-content-viewer/","timezone":"CET"}}') at [Source: output/file.json; line: 1, column: 1] 

感謝您的幫助

回答

0

您顯示的測試json沒有在您的About對象中使用的數組包裝。您也錯過版本字段在您的AboutObject時區字段使用了錯誤的情況。

你的榜樣工作時,我更新了對象:

public class About { 

    private AboutObject about; 

    public AboutObject getAbout() { 
     return about; 
    } 

    public void setAbout(AboutObject about) { 
     this.about = about; 
    } 
} 

public class AboutObject { 

    private String title; 
    private String uri; 
    private String contentViewerUrl; 
    private String timezone; 
    private String version; 

    public String getTitle() { 
     return this.title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getUri() { 
     return this.uri; 
    } 

    public void setUri(String uri) { 
     this.uri = uri; 
    } 

    public String getContentViewerUrl() { 
     return this.contentViewerUrl; 
    } 

    public void setContentViewerUrl(String contentViewerUrl) { 
     this.contentViewerUrl = contentViewerUrl; 
    } 

    public String getTimezone() { 
     return timezone; 
    } 

    public void setTimezone(String timezone) { 
     this.timezone = timezone; 
    } 

    public String getVersion() { 
     return version; 
    } 

    public void setVersion(String version) { 
     this.version = version; 
    } 
} 

測試:

public static void main(String[] args) throws IOException { 
    ObjectMapper mapper = new ObjectMapper(); 
    String obj = "{\"about\":{\"title\":\"NiFi\",\"version\":\"1.1.0\",\"uri\":\"https://localhost:443/api/\",\"contentViewerUrl\":\"/nifi-content-viewer/\",\"timezone\":\"CET\"}}"; 
    About about = mapper.readValue(obj, About.class); 
} 
相關問題