Java的JSON与GSON [英] Java JSON with GSON

查看:132
本文介绍了Java的JSON与GSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的问题,我用来自Wunderground天气API和使用GSON获取天气时遇到了问题。

Here's the problem, I'm using a weather APi from Wunderground and am having trouble using GSON to fetch the weather.

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文件。

That is as far as I got, so can someone please help me to get this thing working. I need to be able to parse specific variables not just the entire JSON file.

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

I used a buffered reader but it read the entire JSON file.

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

Thanks in advance, I'm a beginner so go easy and explain things very straightforward :)

推荐答案

您可以分析整个流,然后拉出你所需要的。下面是分析它的一个例子:

You can parse the entire stream, and then pull out what you need. Here's an example of parsing it:

    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) {    }

阅读它,你必须通过表来浏览你的方式,像这样:

Reading it, you have to navigate your way through the table, something like this:

jsonObject.getJSONObject("Results");

下面是使用这个库我的计划之一的例子:

Here's an example from one of my programs using this library:

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

我大量使用了下列得到它的权利:

I make heavy use out of the following to get it right:

jsonObject.names();

这会给你的所有键的名称。从那里,你必须搞清楚,如果它是一个数组,对象或原始类型。我花了一点得到它的权利,但一旦你做一次它,它永远做,希望。看看 Android的文件上他们的JSON库的。

That will give you the name of all of the keys. From there, you have to figure out if it's an array, an object, or a primitive type. It takes me a bit to get it right, but once you've done it once, it's done forever, hopefully. Take a look at the Android documents on their JSON library.

这篇关于Java的JSON与GSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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