带有GSON的Java JSON

这是问题所在,我正在使用Wunderground的天气APi,并且无法使用GSON来获取天气。

import java.net.*; import java.io.*; import com.google.gson.*; public class URLReader { public static URL link; public static void main(String[] args) { try{ open(); read(); }catch(IOException e){} } public static void open(){ try{ link = new URL("http://api.wunderground.com/api/54f05b23fd8fd4b0/geolookup/conditions/forecast/q/US/CO/Denver.json"); }catch(MalformedURLException e){} } public static void read() throws IOException{ Gson gson = new Gson(); // Code to get variables like humidity, chance of rain, ect... } } 

就我而言,有人可以帮我把这件事搞定。 我需要能够解析特定变量而不仅仅是整个JSON文件。

我使用了缓冲读取器,但它读取了整个JSON文件。

在此先感谢,我是一个初学者,所以很容易解释事情非常简单:)

您可以解析整个流,然后提取您需要的内容。 这是解析它的一个例子:

  String url=getUrl(); JSONObject jsonObject = new JSONObject(); StringBuilder stringBuilder=new StringBuilder(); try { HttpGet httpGet = new HttpGet(url); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(httpGet); HttpEntity entity = response.getEntity(); InputStream stream = entity.getContent(); int b; while ((b = stream.read()) != -1) { stringBuilder.append((char) b); } jsonObject = new JSONObject(stringBuilder.toString()); } catch (JSONException e) { e.printStackTrace(); } catch (ClientProtocolException e) { } catch (IOException e) { } 

阅读它,你必须在表格中导航,如下所示:

 jsonObject.getJSONObject("Results"); 

以下是我使用此库的一个程序的示例:

 int statusCode=jobj.getJSONObject("info").getInt("statuscode"); 

为了正确起见,我大量使用以下内容:

 jsonObject.names(); 

这将为您提供所有键的名称。 从那里,你必须弄清楚它是一个数组,一个对象还是一个原始类型。 我需要一点时间才能做到这一点,但是一旦你完成它,它就会永远完成,希望如此。 看看他们的JSON库上的Android文档 。

GSON可以将JSON树解析为Java对象,Java对象的实例。 因此,如果您想在这种情况下使用GSON,则必须创建类,并使用JSON结构的相关字段名称对它们进行注释。 GSON有一个非常广泛且易于阅读的文档 ,请查看。 但是,对于这种复杂的树,我不建议将其映射到Java对象。 如果您只想要一些数据,请删除GSON并手动导航树,如PearsonArtPhoto所示。

您可以使用JsonParser 。 这是一个基于您的url的示例,您可以立即复制,粘贴和运行。

我添加了一个实用程序方法,允许您使用类似路径的东西从生成的JsonElement树中获取元素。 实际上,您的JSON几乎是一个对象和值的树(除了预测部分)。

请注意, JsonElement可以作为对象,数组或基本值再次“ JsonElement ”为您的需要。 这就是为什么在调用getAtPath ,我调用了getAsString方法。

 package stackoverflow.questions.q19966672; import java.io.*; import java.net.*; import java.nio.charset.Charset; import com.google.gson.*; public class Q19966672 { private static String readAll(Reader rd) throws IOException { BufferedReader reader = new BufferedReader(rd); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } return sb.toString(); } private static JsonElement getAtPath(JsonElement e, String path) { JsonElement current = e; String ss[] = path.split("/"); for (int i = 0; i < ss.length; i++) { current = current.getAsJsonObject().get(ss[i]); } return current; } public static void main(String[] args) { String url = "http://api.wunderground.com/api/54f05b23fd8fd4b0/geolookup/conditions/forecast/q/US/CO/Denver.json"; InputStream is = null; try { is = new URL(url).openStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JsonElement je = new JsonParser().parse(jsonText); System.out.println("In " + getAtPath(je, "current_observation/display_location/city").getAsString() + " is " + getAtPath(je, "current_observation/weather").getAsString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); } catch (IOException e) { e.printStackTrace(); } } } } 

这就是结果:

 In Denver is Mostly Cloudy