0

I am trying to parse json results from a website news feed. However the format seems to be very strange. I have tried to convert it to xml to get a closer look but I'm not sure how to access the fields. Please help

EDIT : I understand that there are different pages that show how to parse it but so far their solutions don't work for me.

EDIT :This is the error I get

java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONObject.getJSONObject(java.lang.String)' on a null object reference

JSON Result (From the browser)

{  
   "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 Converted into XML for better view

<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>

I'd like to access the author, link and other elements at the bottom of the xml but have no idea how to. Here is my android code

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;

My JSONParser class

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;

    }
}
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
JianYA
  • 2,750
  • 8
  • 60
  • 136
  • 1
    What exactly is "very strange" about the format? – Egor May 27 '16 at 13:35
  • You must traverse down the object from the top level elements. You can't immediately access the contents string of the object. And if you want to see cleaner json, then use this site. http://jsonlint.com – OneCricketeer May 27 '16 at 13:36
  • Maybe this could help [Android Parse JSON](http://stackoverflow.com/questions/19746800/android-parse-jsonobject) – Esat IBIS May 27 '16 at 13:37
  • 1
    Have you tried `jsonObject.getJSONObject("responseData").getJSONObject("feed").getString(what_you_want_here)`? – Daniel May 27 '16 at 13:37
  • First of all your `json` is incomplete. Please paste the stacktrace. – Dave Ranjan May 27 '16 at 13:37
  • Sorry will post all the things in an updated edit. And I tried the jsonObject getJSONObject thing. It returns null – JianYA May 27 '16 at 13:42
  • Also, if you want to learn about some libraries to help you with parsing that json, look into using Retrofit – OneCricketeer May 27 '16 at 13:44
  • Oh thanks. But Im trying to learn how to parse this using the tools Android provides atm. – JianYA May 27 '16 at 13:44
  • I updated with newer code. Its still returning null though – JianYA May 27 '16 at 13:48
  • @DaveRanjan, I noticed that as well. My validator threw up a flag & you can tell since there is a comma at the very end too. – Daniel May 27 '16 at 13:49
  • Here is the link for it. I can't put the entire thing up as its too huge. http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=25&q=http://www.abc.net.au/news/feed/51120/rss.xml – JianYA May 27 '16 at 13:52
  • @JianYA, okay that link is valid JSON. You have to tell us what exact element you want so we can help you appropriately. Also, you said you're getting `null`, are you sure that you're actually receiving the JSON. Have you logged it? – Daniel May 27 '16 at 14:02
  • I'd like to access the contents element. I tried logging my JSONParser class to see what Strings it returned but this is what i saw HTTP method POST is not supported by this URL

    HTTP method POST is not supported by this URL

    Error 405

    – JianYA May 27 '16 at 14:07
  • HTTP Status code 405 means `method not allowed`? Why are you using `POST` & not `GET`? – Daniel May 27 '16 at 14:11
  • Oh i found a tutorial that used it. I'll post it on the question. – JianYA May 27 '16 at 14:13
  • Well try `HttpGet` instead or try something entirely different since those methods were deprecated. – Daniel May 27 '16 at 14:19
  • I'll see what I can do. Thanks. – JianYA May 27 '16 at 14:27

0 Answers0