将jersey/jackson json分为几类 [英] Get jersey/jackson json into several classes

查看:109
本文介绍了将jersey/jackson json分为几类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器收到此json响应.

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?

代码

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

推荐答案

我尝试使用Jackson,并且能够在一个内衬中构建JSON对象.可能是您要寻找的东西.

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

}

输出:

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

这篇关于将jersey/jackson json分为几类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆