Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

java - Get jersey/jackson json into several classes

I have this json response from a server.

{"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}}

The top level info is parsed fine into the below class. But how do I populate the list of server info?

The code

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;

}

This works, but I would hope there is a way to convert it in one step since there might be more nested levels in other responses.

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I tried using Jackson, and was able to build the JSON object in one liner. Probably what you are looking for.

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;
    }

}

Output:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...