2015-06-22 43 views
0

我有一個來自服務器的json響應。將jersey/jackson json分爲幾個類

{"session_key":"thekey","expires_in":300,"environment":"exttest","country":"SE","private_feed":{"hostname":"priv.api.test.nordnet.se","port":443,"encrypted":true},"public_feed":{"hostname":"pub.api.test.nordnet.se","port":443,"encrypted":true}} 

頂級信息被細分到下面的類中。但是,我如何填充服務器信息列表?

代碼

Response response = baseResource.path("login").queryParam("service", "NEXTAPI") 
       .queryParam("auth", authParam).request(responseType).post(null); 
     System.out.println(response); 

     SessionInfo ses = response.readEntity(SessionInfo.class); 

public class SessionInfo { 
    public String session_key; 
    public String environment; 
    public int expires_in; 
    public String country; 

    List<ServerInfo> serverInfo = new ArrayList<ServerInfo>(); 
} 

public class ServerInfo { 
    public String hostname; 
    public int port; 
    public boolean encrypted; 

} 

這工作,但我希望有一種方法將其轉換一步到位,因爲有可能是其它響應多個嵌套的水平。

ObjectMapper mapper = new ObjectMapper(); 

     ObjectNode json = response.readEntity(ObjectNode.class); 

     SessionInfo ses = mapper.treeToValue(json, SessionInfo.class); 
     ServerInfo s1 = mapper.treeToValue(json.get("private_feed"), ServerInfo.class); 
     ServerInfo s2 = mapper.treeToValue(json.get("public_feed"), ServerInfo.class); 
     ses.serverInfo.add(s1); 
     ses.serverInfo.add(s2); 
+0

轉換成JSON你的反應,並獲得價值 – Garry

+0

當然,我可以手動解析它。但是,如果有這樣一個很好的功能只讀入課程,我想盡可能使用它。 – user2130951

回答

0

我試着用傑克遜,並能夠構建JSON對象一行。可能你在找什麼。

import java.io.IOException; 

import org.codehaus.jackson.JsonGenerationException; 
import org.codehaus.jackson.annotate.JsonProperty; 
import org.codehaus.jackson.map.JsonMappingException; 
import org.codehaus.jackson.map.ObjectMapper; 


public class JackSontest { 

    public static void main(String[] args){ 
     String jstr = "{\"session_key\":\"thekey\",\"expires_in\":300,\"environment\":\"exttest\",\"country\":\"SE\",\"private_feed\":{\"hostname\":\"priv.api.test.nordnet.se\",\"port\":443,\"encrypted\":true},\"public_feed\":{\"hostname\":\"pub.api.test.nordnet.se\",\"port\":443,\"encrypted\":true}}"; 
    System.out.println("Calling jsonToObject..."); 
     ObjectMapper objectMapper = new ObjectMapper(); 
     try { 

      SessionInfo info = objectMapper.readValue(jstr, SessionInfo.class); 
     System.out.println("Session_key:- " + info.getSession_key()); 
     System.out.println("Expires_in:- " + info.getExpires_in()); 
     System.out.println("Environment:- " + info.getEnvironment()); 
     System.out.println("Private Feed:- " + info.getPrivate_feed().getHostname()); 
     System.out.println("Public Feed:- " + info.getPublic_feed().getHostname()); 

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

class SessionInfo { 
    private String session_key; 
    private String environment; 
    private int expires_in; 
    public String getSession_key() { 
     return session_key; 
    } 
    public void setSession_key(String session_key) { 
     this.session_key = session_key; 
    } 
    public String getEnvironment() { 
     return environment; 
    } 
    public void setEnvironment(String environment) { 
     this.environment = environment; 
    } 
    public int getExpires_in() { 
     return expires_in; 
    } 
    public void setExpires_in(int expires_in) { 
     this.expires_in = expires_in; 
    } 
    public String getCountry() { 
     return country; 
    } 
    public void setCountry(String country) { 
     this.country = country; 
    } 

    private String country; 


    private Feed private_feed; 

    public Feed getPrivate_feed() { 
     return private_feed; 
    } 

    @JsonProperty("private_feed") 
    public void setPrivate_feed(Feed private_feed) { 
     this.private_feed = private_feed; 
    } 

    private Feed public_feed; 

    public Feed getPublic_feed() { 
     return public_feed; 
    } 

    @JsonProperty("public_feed") 
    public void setPublic_feed(Feed public_feed) { 
     this.public_feed = private_feed; 
    } 
} 

class Feed { 
    private String hostname; 
    private int port; 
    private boolean encrypted; 
    public String getHostname() { 
     return hostname; 
    } 
    public void setHostname(String hostname) { 
     this.hostname = hostname; 
    } 
    public int getPort() { 
     return port; 
    } 
    public void setPort(int port) { 
     this.port = port; 
    } 
    public boolean isEncrypted() { 
     return encrypted; 
    } 
    public void setEncrypted(boolean encrypted) { 
     this.encrypted = encrypted; 
    } 

} 

輸出:

Calling jsonToObject... 
Session_key:- thekey 
Expires_in:- 300 
Environment:- exttest 
Private Feed:- priv.api.test.nordnet.se 
Public Feed:- priv.api.test.nordnet.se 
+0

忽略了@JsonProperty(「public_feed」) – user2130951

0

你試過GSON:

public class Employee 
{ 
    private Integer id; 
    private String firstName; 
    private String lastName; 
    private List<String> roles; 
    private Department department; //Department reference 

    //Other setters and getters 
} 

class DepartmentInstanceCreator implements InstanceCreator<Department> { 
    public Department createInstance(Type type) 
    { 
     return new Department("None"); 
    } 
} 

//Now <strong>use the above InstanceCreator</strong> as below 

GsonBuilder gsonBuilder = new GsonBuilder(); 
gsonBuilder.registerTypeAdapter(Department.class, new DepartmentInstanceCreator()); 
Gson gson = gsonBuilder.create(); 

System.out.println(
      gson.fromJson("{'id':1,'firstName':'Lokesh','lastName':'Gupta','roles':['ADMIN','MANAGER'],'department':{'deptName':'Finance'}}", 
      Employee.class)); 

輸出:

Employee [id=1, firstName=Lokesh, lastName=Gupta, roles=[ADMIN, MANAGER], department=Department [deptName=Finance]] 

source

+0

是的,但是當你解析響應時,它確實是倒退了。 – user2130951