如何从网站解析JSON

时间:2016-05-27 13:32:00

标签: android json xml

我正在尝试解析网站新闻Feed中的json结果。然而格式似乎很奇怪。我试图将其转换为xml以进行仔细观察,但我不确定如何访问这些字段。请帮忙

编辑:我知道有不同的页面显示如何解析它,但到目前为止他们的解决方案对我不起作用。

编辑:这是我得到的错误

  

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'org.json.JSONObject org.json.JSONObject.getJSONObject(java.lang.String)'

JSON结果(来自浏览器)

{  
   "responseData":{  
      "feed":{  
         "feedUrl":"http://www.abc.net.au/news/feed/51120/rss.xml",
         "title":"Just In",
         "link":"http://www.abc.net.au/news/justin/",
         "author":"",
         "description":"",
         "type":"rss20",
         "entries":[  
            {  
               "mediaGroups":[  
                  {  
                     "contents":[  
                        {  
                           "url":"http://www.abc.net.au/news/image/7388850-16x9-2150x1210.jpg",
                           "type":"image/jpeg",
                           "medium":"image",
                           "height":1210,
                           "width":2150,
                           "lang":"en-US",
                           "description":"New Zealand netball could actually benefit from its split from Australia. (Getty Images: Mead Norton)",
                           "thumbnails":[  
                              {  
                                 "height":105,
                                 "width":140,
                                 "url":"http://www.abc.net.au/news/image/7388850-4x3-140x105.jpg"
                              }
                           ]
                        },
                        {  
                           "url":"http://www.abc.net.au/news/image/7388850-4x3-940x705.jpg",
                           "type":"image/jpeg",
                           "medium":"image",
                           "height":705,
                           "width":940,
                           "lang":"en-US",
                           "description":"New Zealand netball could actually benefit from its split from Australia. (Getty Images: Mead Norton)",
                           "thumbnails":[  
                              {  
                                 "height":105,
                                 "width":140,
                                 "url":"http://www.abc.net.au/news/image/7388850-4x3-140x105.jpg"
                              }
                           ]
                        }}]}}, "responseDetails": null, "responseStatus": 200}

JSON转换为XML以获得更好的视图

<root>
<responseData>
    <feed>
        <feedUrl>http://www.abc.net.au/news/feed/51120/rss.xml</feedUrl>
        <title>Just In</title>
        <link>http://www.abc.net.au/news/justin/</link>
        <author></author>
        <description></description>
        <type>rss20</type>
        <entries>
            <mediaGroups>
                <contents>
                    <url>http://www.abc.net.au/news/image/7388850-16x9-2150x1210.jpg</url>
                    <type>image/jpeg</type>
                    <medium>image</medium>
                    <height>1210</height>
                    <width>2150</width>
                    <lang>en-US</lang>
                    <description>New Zealand netball could actually benefit from its split from Australia. (Getty Images: Mead Norton)</description>
                    <thumbnails>
                        <height>105</height>
                        <width>140</width>
                        <url>http://www.abc.net.au/news/image/7388850-4x3-140x105.jpg</url>
                    </thumbnails>
                </contents>
                <contents>
                    <url>http://www.abc.net.au/news/image/7388850-4x3-940x705.jpg</url>
                    <type>image/jpeg</type>
                    <medium>image</medium>
                    <height>705</height>
                    <width>940</width>
                    <lang>en-US</lang>
                    <description>New Zealand netball could actually benefit from its split from Australia. (Getty Images: Mead Norton)</description>
                    <thumbnails>
                        <height>105</height>
                        <width>140</width>
                        <url>http://www.abc.net.au/news/image/7388850-4x3-140x105.jpg</url>
                    </thumbnails>
                </contents>
                <contents>
                    <url>http://www.abc.net.au/news/image/7388850-3x2-940x627.jpg</url>
                    <type>image/jpeg</type>
                    <medium>image</medium>
                    <isDefault>true</isDefault>
                    <height>627</height>
                    <width>940</width>
                    <lang>en-US</lang>
                    <description>New Zealand netball could actually benefit from its split from Australia. (Getty Images: Mead Norton)</description>
                    <thumbnails>
                        <height>105</height>
                        <width>140</width>
                        <url>http://www.abc.net.au/news/image/7388850-4x3-140x105.jpg</url>
                    </thumbnails>
                </contents>
                <contents>
                    <url>http://www.abc.net.au/news/image/7388850-3x4-940x1253.jpg</url>
                    <type>image/jpeg</type>
                    <medium>image</medium>
                    <height>1253</height>
                    <width>940</width>
                    <lang>en-US</lang>
                    <description>New Zealand netball could actually benefit from its split from Australia. (Getty Images: Mead Norton)</description>
                    <thumbnails>
                        <height>105</height>
                        <width>140</width>
                        <url>http://www.abc.net.au/news/image/7388850-4x3-140x105.jpg</url>
                    </thumbnails>
                </contents>
                <contents>
                    <url>http://www.abc.net.au/news/image/7388850-1x1-1400x1400.jpg</url>
                    <type>image/jpeg</type>
                    <medium>image</medium>
                    <height>1400</height>
                    <width>1400</width>
                    <lang>en-US</lang>
                    <description>New Zealand netball could actually benefit from its split from Australia. (Getty Images: Mead Norton)</description>
                    <thumbnails>
                        <height>105</height>
                        <width>140</width>
                        <url>http://www.abc.net.au/news/image/7388850-4x3-140x105.jpg</url>
                    </thumbnails>
                </contents>
            </mediaGroups>
            <title>New Zealand netball to get stronger after trans-Tasman split</title>
            <link>http://www.abc.net.au/news/2016-05-27/new-zealand-netball-to-get-stronger-after-trans-tasman-split/7454468</link>
            <author>Brittany Carter</author>
            <publishedDate>Thu, 26 May 2016 23:27:34 -0700</publishedDate>
            <contentSnippet>New Zealand's netball split with Australia has many fearing the worst, but it could well prove a massive boost for the sport ...</contentSnippet>
            <content>
                <p>New Zealand's netball split with Australia has many fearing the worst, but it could well prove a massive boost for the sport across the ditch.</p>
            </content>
            <categories>Netball</categories>
            <categories>Sport</categories>
        </entries>

我想访问xml底部的作者,链接和其他元素,但不知道如何。这是我的安卓代码

try{
            //instantiate json parser
            JSONParser jsonParser = new JSONParser();
            //get json string from the url
            JSONObject jsonObject = jsonParser.getJSONFromURL(jsonURL);
 Log.d("JSONOUT",jsonObject.getJSONObject("responseData").getJSONObject("feed").getString("feedUrl"));
        }catch (JSONException e){
            e.printStackTrace();
        }
        return null;

我的JSONParser类

public class JSONParser {
static InputStream is = null;
static JSONObject jsonObject = null;
static String json = "";

public JSONObject getJSONFromURL(String url){
    //start HTTP Request
    try{
        DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = defaultHttpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    }catch (UnsupportedEncodingException e){
        e.printStackTrace();
    }catch (ClientProtocolException e){
        e.printStackTrace();
    }catch (IOException e){
        e.printStackTrace();
    }
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line=reader.readLine())!=null){
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
        Log.d("JSONRECEIVE",json);
    }catch (UnsupportedEncodingException e){
        e.printStackTrace();
    }catch (IOException e){
        e.printStackTrace();
    }

    //parse received String to a JSON object
    try{
        jsonObject = new JSONObject(json);
    }catch (JSONException e){
        e.printStackTrace();
    }
    //return the completed string
    return jsonObject;

    }
}

0 个答案:

没有答案