用 Java 解析 JSON 数据 [英] Parsing JSON data in Java

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

问题描述

我想从这个页面解析一些数据:http://www.bbc.co.uk/radio1/programmes/schedules/england/2013/03/1.json

I want to parse the some data from this page: http://www.bbc.co.uk/radio1/programmes/schedules/england/2013/03/1.json

我要解析的数据是标题,但我不确定如何提取数据.这是我到目前为止所做的:

The data I want to parse is the titles however I am unsure how I can extract the data. This is what I have done so far:

 import java.io.BufferedReader;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import org.json.simple.JSONObject;
 import org.json.simple.parser.JSONParser;

 public class Test
 {
    public Test() { }

    public static void main(String[] args)
    {
            URL url;
            HttpURLConnection connection = null;
            InputStream is = null;
            JSONParser parser = new JSONParser();

            try
            {
                    url = new URL("http://www.bbc.co.uk/radio1/programmes/schedules/england/2013/03/1.json");
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.connect();
                    is = connection.getInputStream();
                    BufferedReader theReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    String reply;
                    while ((reply = theReader.readLine()) != null)
                    {
                            System.out.println(reply);
                            Object obj = parser.parse(reply);
                            JSONObject jsonObject = (JSONObject) obj;
                            String title = (String) jsonObject.get("time");
                            System.out.println(title);
                    }
            }
            catch (Exception e) {
                    e.printStackTrace();
            }
    }

}

这只是返回空值.谁能告诉我我需要改变什么?谢谢.

This just returns null. Can anybody tell me what I need to change? Thanks.

推荐答案

如果你阅读了 JSONObject#get(String) 的 javadoc,它实际上是 HashMap.get(String)代码>,它说

If you read the javadoc of JSONObject#get(String) which is actually HashMap.get(String), it states

返回:指定键映射到的值,如果是null此映射不包含键的映射

Returns: the value to which the specified key is mapped, or null if this map contains no mapping for the key

您的 JSON 不包含键 time 的映射.

Your JSON does not contain a mapping for the key time.

如果您指的是 title 而不是 time,请获取 JSON 的摘录

If you meant title instead of time, take this extract of the JSON

{"schedule":{"service":{"type":"radio","key":"radio1","title":"BBC Radio 1",...

您需要首先将 schedule 作为 JSONObject,然后将 service 作为 JSONObject,然后 title 作为一个普通的 String 值.根据 JSON 值的类型应用不同的方法.

You need to first get schedule as a JSONObject, then service as a JSONObject, and then title as a normal String value. Apply this differently depending on the type of JSON value.

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

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