Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.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
从JSON读取Java_Java_Rest - Fatal编程技术网

从JSON读取Java

从JSON读取Java,java,rest,Java,Rest,我希望从我在Netbeans Java项目中使用的天气API中获取天气信息,并将其输入到我自己的Maven API中,这一切都很好,但当我想将其分解为更小的可读文本时,会返回一个巨大的大块JSON响应 我的代码当前正在返回: {“coord”:{“lon”:-6.26,“lat”:53.35},天气:[{“id”:801,主要:“云”,“描述”:“很少” “云”,“图标”:“02d”}],“基地”:“站”,“主”;“温度”;“285.59”,“压力”:1015,“湿度”:58,“温度最低”:28

我希望从我在Netbeans Java项目中使用的天气API中获取天气信息,并将其输入到我自己的Maven API中,这一切都很好,但当我想将其分解为更小的可读文本时,会返回一个巨大的大块JSON响应

我的代码当前正在返回:

{“coord”:{“lon”:-6.26,“lat”:53.35},天气:[{“id”:801,主要:“云”,“描述”:“很少” “云”,“图标”:“02d”}],“基地”:“站”,“主”;“温度”;“285.59”,“压力”:1015,“湿度”:58,“温度最低”:285.15,“温度最高”:286.15},“能见度”:10000,“风”;“速度”:3.6,“度”:80},“云”;“所有”:20},“dt”:1539610200,“系统”;“类型”:1,“id”:5237,“信息”:0.0027,“国家”:“IE”,“日出”:15395867,“日落”:15396249},“id”:2964574,“名称”:“都柏林”,“cod”:200}

相反,我希望它能够返回,主要在天气和温度以及天气。如果有人有任何想法让我知道。代码是附加的

public class WeatherInfo {
     public static String getWeather(String city) {
        String weather;
        String getUrl = "http://api.openweathermap.org/data/2.5/weather?q="+ city +"&appid=xxx";
        Client client = Client.create();
        WebResource target = client.resource(getUrl);

        ClientResponse response = target.get(ClientResponse.class);
        weather = response.getEntity(String.class);
        return weather;
}
}

您需要使用一个库来解析JSON响应。下面是一个包含大量示例和参考的SO问题:

用户SDekov的回答列出了三个好的选项:

  • 谷歌GSON
  • Org.JSON
  • 杰克逊

我假设您希望从
getWeather
返回的值是
{“main”:“Clouds”,“temp”:285.59}
这里有一个解决方案- 在pom中添加jackson依赖项

 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.8.7</version>
</dependency>

com.fasterxml.jackson.core
杰克逊数据绑定
2.8.7
这里有一个方法,它去掉了其他细节,只返回main和temp,你可以编辑这个方法来添加更多的字段

private static String getLessWeather(String weatherJson) throws IOException {
  Map<String, Object> lessWeatherMap = new LinkedHashMap<String, Object>();
  Map<String,Object> weatherMap = new ObjectMapper().readValue(weatherJson, LinkedHashMap.class);
  String main = (String) ((LinkedHashMap)((ArrayList)weatherMap.get("weather")).get(0)).get("main");
  lessWeatherMap.put("main", main);
  Double temp = (Double)((LinkedHashMap)weatherMap.get("main")).get("temp");
  lessWeatherMap.put("temp", temp);
  return new ObjectMapper().writeValueAsString(lessWeatherMap);
}
private静态字符串getLessWeather(字符串weatherJson)抛出IOException{
Map lesshoothermap=newlinkedhashmap();
Map weatherMap=newObjectMapper().readValue(weatherJson,LinkedHashMap.class);
stringmain=(String)((LinkedHashMap)((ArrayList)weatherMap.get(“天气”)).get(0)).get(“主”);
LESP.put(“主要”,主要);
Double temp=(Double)((LinkedHashMap)weatherMap.get(“main”)).get(“temp”);
less.p.put(“temp”,temp);
返回新的ObjectMapper().writeValueAsString(lesMap);
}

你所说的“天气中的主要温度和天气中的温度”是什么意思"? 你能给出所需的json吗?@noobCoder更新了我上面的json,并将我希望能够读取/输出的内容加粗,现在我可以使用json对象查看main-temp,然后使用toDouble查看temp,但我无法查看weather-main。好的,让我知道我发布的答案是否适合你。