Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,705 members, 7,955,663 topics. Date: Sunday, 22 September 2024 at 12:12 PM |
Nairaland Forum / Science/Technology / Programming / Exception While Reading JSON Object In Java (1885 Views)
Android Quiz Application With Json Parser, Php And Mysql Database / Tutorial: How To Return Html In Json Response Using Asp.net And Jquery / Exception In Thread "main" NoclassDefFoundError; Class Name (2) (3) (4)
Exception While Reading JSON Object In Java by participantjava: 5:16am On Feb 20, 2013 |
Hi Team, Iam trying to read JSON object using java technology, but facing the exception.Below is the JSON object i want to read: {"website":"http://developers.facebook.com","link":"http://www.facebook.com/FacebookDevelopers","about":"Grow your app with Facebook\nhttps://developers.facebook.com/ ","id":"19292868552","username":"FacebookDevelopers","category":"Product/service","cover":{"source":"http://sphotos-b.xx.fbcdn.net/hphotos-ash4/s720x720/377655_10151298218353553_500025775_n.png","cover_id":"10151298218353553","offset_y":0},"name":"Facebook Developers","likes":1138263,"were_here_count":0,"company_overview":"Facebook Platform enables anyone to build social apps on Facebook, mobile, and the web.\n\n","is_published":true,"talking_about_count":32452} Exception is as below : org.json.JSONException: JSONArray[6] not a string. Can you please help me how to get key value pairs out of the JSON String given above using java technology. Thanks inadvance. |
Re: Exception While Reading JSON Object In Java by naijaswag1: 10:31am On Feb 20, 2013 |
Invoked google to come up with examples. Couldn't get at any clear and straightforward solution, so I had to conjure something. I have read about JSON courtesy of coreservlets and also when I did read some J2EE tutorials on web services. Here we go, I could read the file, the content is okay, didn't get any "org.json.JSONException: JSONArray[6] not a string." I tried to find a library I could use to read the key-value pairs to any depth, couldn't find any in the limited time I have, just wanted to exercise my brain a little. I have a solution though but it is clumsy and convoluted and sloppy. Here it goes, mind you, you will need json-simple jar from googlecode http://code.google.com/p/json-simple/downloads/list to get cracking. In other to read the key value pairs to any length, I have to implore one of the most fundamental concepts of programming,recursion to dig in and get the stuffs. I ran into another problem, how do I tell which key-value exist up the recursion tree? You will see what happens in the code and result.I don't now what you want to do with the key-value pairs that will determine the solution you want. import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Set; import org.json.simple.JSONArray; import org.json.JSONException; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class ReadFacebook { /** * @param args * @throws ParseException * @throws IOException * @throws FileNotFoundException * @throws JSONException */ public static void main(String[] args) throws FileNotFoundException, IOException, ParseException, JSONException { // TODO Auto-generated method stub JSONParser parser = new JSONParser(); JSONArray a = (JSONArray) parser.parse(new FileReader(System .getProperty("user.dir" + "/src/coreservlets/nairaland.json"); JSONObject obj2 = (JSONObject) a.get(0); Set<Object> s = obj2.keySet(); String key = null; for (Object o : s) { printRecursive(o,o,obj2.get(o).toString()); } } static void printRecursive(Object mainkey, Object ob,String s) throws ParseException { if (!s.startsWith("{" && !s.endsWith("}") { System.out.println("MainKey :" + mainkey); System.out.println("key :" + ob); System.out.println("value :" + s); System.out.println(); return; } JSONParser parser = new JSONParser(); JSONObject obj = (JSONObject) parser.parse( s); Set<Object> ss = obj.keySet(); String key = null; for (Object o : ss) { printRecursive(mainkey.toString(),o,obj.get(o).toString()); } } } Sample output MainKey :website key :website value :http://developers.facebook.com MainKey :link key :link value :http://www.facebook.com/FacebookDevelopers MainKey :about key :about value :Grow your app with Facebook https://developers.facebook.com/ MainKey :id key :id value :19292868552 MainKey :username key :username value :FacebookDevelopers MainKey :category key :category value :Product/service MainKey :cover key :source value :http://sphotos-b.xx.fbcdn.net/hphotos-ash4/s720x720/377655_10151298218353553_500025775_n.png MainKey :cover key :cover_id value :10151298218353553 MainKey :cover key :offset_y value :0 MainKey :name key :name value :Facebook Developers MainKey :likes key :likes value :1138263 MainKey :were_here_count key :were_here_count value :0 MainKey :company_overview key :company_overview value :Facebook Platform enables anyone to build social apps on Facebook, mobile, and the web. MainKey :is_published key :is_published value :true MainKey :talking_about_count key :talking_about_count value :32452 Developer environment is eclipse indigo. |
Re: Exception While Reading JSON Object In Java by sunnyben: 12:44pm On Feb 21, 2013 |
I don't know the library you are using to parse the json string. But the one i've used before is gson. Maybe you can try it out. |
Re: Exception While Reading JSON Object In Java by participantjava: 10:48pm On Feb 23, 2013 |
Ok ..thanks you. I was using json-simple.jar file. Can you please suggest using java how can i create a talking tom like application? Which API in java should i use to create a talking tom like application? Thanks in advance. Thanks. |
(1) (Reply)
.NET Vs Python For Cryptography And Cybersecurity / Webmasters Please Help With This Problem / Recommended Software Or Programming Training For An Electrical Student
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 19 |