problem with JSON parsing

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

problem with JSON parsing

Postby irfan123 » Wed Dec 02, 2009 9:28 am

Can anyone tell me how to parse this JSON output which is as following

handleReutersJson({
"title": "Reuters | Top News",
"link": "http://www.reuters.com",
"id": "http://spotlight.reuters.com/api/feed/us/channelarticles/topnews/json",
"description": "Reuters | Top News",
"copyright": "© 2009 Reuters",
"image": {
"title": "Reuters News",
"width": 120,
"height": 35,
"link": "http://www.reuters.com",
"url": "http://i.today.reuters.com/images/reuters120.gif"
})

can anyone help me out how to extract data when there JSON output in this format.
Need help to build android application
irfan123
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sun Oct 11, 2009 2:18 am
Location: San Diego

Top

Postby burakkilic » Thu Dec 03, 2009 11:05 am

There are libraries for Java which can do that. look at JSON.org.
burakkilic
Developer
Developer
 
Posts: 37
Joined: Thu Oct 01, 2009 10:24 am

Postby padde » Thu Dec 03, 2009 3:38 pm

Those libs are already included in android.
By the way that JSON object looks wrong..
a closing curly bracket is missing.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby tbennett » Fri Dec 04, 2009 12:31 am

assuming the correct JSON was this:

{
handleReutersJson {
"title": "Reuters | Top News",
"link": "http://www.reuters.com",
"id": "http://spotlight.reuters.com/api/feed/us/channelarticles/topnews/json",
"description": "Reuters | Top News",
"copyright": "© 2009 Reuters",
"image": {
"title": "Reuters News",
"width": 120,
"height": 35,
"link": "http://www.reuters.com",
"url": "http://i.today.reuters.com/images/reuters120.gif"
}
}
}

... you could do this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. String title, link, description, imageTitle;
  3.  
  4. int imageHeight, imageWidth;
  5.  
  6.  
  7.  
  8. title = link = description = imageTitle = null;
  9.  
  10. imageWidth = imageHeight = 0;
  11.  
  12. try {
  13.  
  14.         final JSONObject json = new JSONObject(<your JSON string>);
  15.  
  16.        
  17.  
  18.         title = json.optString("title","no title");
  19.  
  20.         link = json.optString("link","no link");
  21.  
  22.         description = json.optString("description","no description");
  23.  
  24.         //... and so on with the other fields you want to use
  25.  
  26.        
  27.  
  28.         if(json.has("image")) {
  29.  
  30.                 final JSONObject jsonImage = json.getJSONObject("image");
  31.  
  32.                
  33.  
  34.                 imageTitle = jsonImage.optString("title", "no image title");
  35.  
  36.                 imageWidth = jsonImage.optInt("width", 0);
  37.  
  38.                 imageHeight = jsonImage.optInt("height", 0);
  39.  
  40.                 //... and so on with the other fields you want to use
  41.  
  42.         }
  43.  
  44.  
  45.  
  46.         //... do whatever you're doing w/the data you parsed
  47.  
  48.        
  49.  
  50. } catch (JSONException e) {
  51.  
  52.         e.printStackTrace();
  53.  
  54.  
  55.  
  56. }
  57.  
  58.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
tbennett
Junior Developer
Junior Developer
 
Posts: 13
Joined: Sun Dec 07, 2008 7:58 am

Ultimately got it......

Postby irfan123 » Fri Dec 04, 2009 6:01 am

The code that i hve written is JSON feed and is absolutely the correct code. Nothing is missing.

What i had figured out after 4 days is that.........it is a callback method. We had to define a default method in our code. And the JSON feed wud b automatically replied back through the callback method. Then we can access all the information of the JSON feed from the method(which we defined) in our program.

Actually this JSON callback feed only works with JavaScript but not with java.
Need help to build android application
irfan123
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sun Oct 11, 2009 2:18 am
Location: San Diego

Re: problem with JSON parsing

Postby sarangkulkarni17 » Tue Feb 09, 2010 10:34 am

irfan123 wrote:Can anyone tell me how to parse this JSON output which is as following

handleReutersJson({
"title": "Reuters | Top News",
"link": "http://www.reuters.com",
"id": "http://spotlight.reuters.com/api/feed/us/channelarticles/topnews/json",
"description": "Reuters | Top News",
"copyright": "© 2009 Reuters",
"image": {
"title": "Reuters News",
"width": 120,
"height": 35,
"link": "http://www.reuters.com",
"url": "http://i.today.reuters.com/images/reuters120.gif"
})

can anyone help me out how to extract data when there JSON output in this format.


Hello everybody, I know this is old post but it will helps other.

In above response the curly brace { } represent object & square bracket [ ] represent array.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public JsonParser(String string_Url) {
  2. java.net.URL url = new java.net.URL(string_Url);
  3.  
  4.                         java.net.URLConnection conn = (java.net.URLConnection) url
  5.                                         .openConnection();
  6.                         conn.setDoOutput(true);
  7.                         conn.setDoInput(true);
  8.  
  9.                         String line = Developer
  10.                                         .convertStreamToString(conn.getInputStream());
  11.  
  12.                         Log.d("RES IS ", line);
  13.                        
  14.                         final JSONObject json = new JSONObject(line);
  15.  
  16.         checkForJSONObject(json);
  17. }
  18.  
  19. /**
  20.          * This is recursive function used for getting key value pair from array or
  21.          * object In this nameArray gives field names & valArray gives contents
  22.          * */
  23.         private void checkForJSONObject(JSONObject jsonObject) {
  24.                 try {
  25.                         JSONArray nameArray = jsonObject.names();
  26.                         JSONArray valArray = jsonObject.toJSONArray(nameArray);
  27.                        
  28.                         for (int i = 0; i < jsonObject.length(); i++) {
  29.  
  30.                                 // whenever there is array then control comes here
  31.                                 if (valArray.get(i).getClass().isInstance(new JSONArray())) {
  32.                                         JSONArray arr = jsonObject.getJSONArray(nameArray.get(i)
  33.                                                         .toString());
  34.  
  35.                                         for (int j = 0; j < arr.length(); j++) {
  36.                                                 if (nameArray.get(i).equals("Response")) {
  37.                                                         int_responseCount++;
  38.                                                         System.out.println("**************************** "+int_responseCount);
  39.                                                 }
  40.  
  41.                                                 Object oo = arr.get(j);
  42.                                                 if (oo.getClass().isInstance(new JSONArray())) {
  43.                                                         System.out.println("IS ARRAY " + arr.get(j));
  44.                                                 } else if (oo.getClass().isInstance(new JSONObject())) {
  45.                                                         checkForJSONObject((JSONObject) oo);
  46.                                                 } else {
  47.                                                         System.out.println("name "
  48.                                                                         + nameArray.get(i).toString() + " con "
  49.                                                                         + arr.get(j).toString());
  50.                                                        
  51.                                                 }
  52.  
  53.                                         }
  54.                                 } else if (valArray.get(i).getClass().isInstance(
  55.                                                 new JSONObject())) {
  56.                                         checkForJSONObject((JSONObject) valArray.get(i));
  57.                                 } else {
  58.                                        
  59.  
  60.                                         System.out.println(" NAME " + nameArray.get(i)
  61.                                                         + "  CONTENT " + valArray.get(i));
  62.                                 }
  63.                         }
  64.                 } catch (Exception e) {
  65.                         System.out.println("Ex in checkForJsonObj " + e.toString());
  66.                         e.printStackTrace();
  67.                 }
  68.  
  69.         }
  70.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Any suggestion from u is welcome.

I hope this will helpful for someone.
sarangkulkarni17
Developer
Developer
 
Posts: 29
Joined: Fri Feb 13, 2009 10:00 am
Location: Pune

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 3 guests