Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将jersey/jackson json分为几个类_Java_Json_Jersey - Fatal编程技术网

Java 将jersey/jackson json分为几个类

Java 将jersey/jackson json分为几个类,java,json,jersey,Java,Json,Jersey,我有来自服务器的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,"encrypte

我有来自服务器的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;

}
您是否尝试过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));

我尝试使用Jackson,并且能够在一行代码中构建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;
    }

}
输出:

Employee [id=1, firstName=Lokesh, lastName=Gupta, roles=[ADMIN, MANAGER], department=Department [deptName=Finance]]
Calling jsonToObject...
Session_key:- thekey
Expires_in:- 300
Environment:- exttest
Private Feed:- priv.api.test.nordnet.se
Public Feed:- priv.api.test.nordnet.se

将您的响应转换为JSON并获取值,当然我可以手动解析它。但是当有一个很好的特性可以读入类时,如果可能的话,我会使用它。是的,但是当你解析响应时,它实际上是向后的。忽略了@JsonProperty(“public_feed”)
Calling jsonToObject...
Session_key:- thekey
Expires_in:- 300
Environment:- exttest
Private Feed:- priv.api.test.nordnet.se
Public Feed:- priv.api.test.nordnet.se