Println只有两个来自JSON的值

时间:2015-08-18 10:02:00

标签: java json jersey-client

我使用Jersey客户端阅读有关国家/地区的json信息。

Client client = Client.create();
String getUrl = "http://restcountries.eu/rest/v1/name/norway";

WebResource webResource = client.resource(getUrl);
ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
if(response.getStatus()!=200){
    throw new RuntimeException("HTTP Error: "+ response.getStatus());
}

String result = response.getEntity(String.class);
System.out.println("Response from the Server: ");
System.out.println(result);

但我的代码会返回所有信息,就像在网页上一样:

[{"name":"Norway","capital":"Oslo","altSpellings":["NO","Norge","Noreg","Kingdom of Norway","Kongeriket Norge","Kongeriket Noreg"],"relevance":"1.5","region":"Europe","subregion":"Northern Europe","translations":{"de":"Norwegen","es":"Noruega","fr":"Norvège","ja":"ノルウェー","it":"Norvegia"},"population":5156450,"latlng":[62.0,10.0],"demonym":"Norwegian","area":323802.0,"gini":25.8,"timezones":["UTC+01:00"],"borders":["FIN","SWE","RUS"],"nativeName":"Norge","callingCodes":["47"],"topLevelDomain":[".no"],"alpha2Code":"NO","alpha3Code":"NOR","currencies":["NOK"],"languages":["no","nb","nn"]}]

我想询问是否存在任何方式只能打印#34;名称"和"资本"。

1 个答案:

答案 0 :(得分:1)

您可以将响应转换为java对象,而只能使用那些非常有用的参数。

请参阅本教程:http://examples.javacodegeeks.com/core-java/xml/bind/jaxb-json-example/

它有一个从java转换为json和json转换为java的例子