Results 1 to 9 of 9
- 11-12-2010, 06:18 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 14
- Rep Power
- 0
How to read/output a JSON as a normal String variable
Could someone please tell me how to convert JSON to a String variable so I can read it.
At the moment, my code is like this:
URL urban = new URL("http://www.urbandictionary.com/iphone/search/define?term=jad");
URLConnection yc = urban.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
int index_of = in.readLine().indexOf("n\":"); // dont worry about this line
System.out.println("index: "+index_of); //dont worry bout this
String output= in.readLine().toString();
System.out.println("Sentence output: "+output);
This is the output I get:
[][][][][][][][][][][][][][].
And I know the json has something in it because I opened it up in notepad. At the moment I am reading about json etc. but would like some input how I can store it as a string to view and read.Last edited by Jman85; 11-12-2010 at 06:20 AM.
- 11-12-2010, 08:44 AM #2
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
I have to guess this but I think the problem is in the first line you don't want us to worry about :).
It has a readLine in it and this makes the input stream empty. So the next readLine does not read anything since the JSON string is one single line (I opened it in Notepad to check).
my suggestion, something like:
Another suggestion: google for a JSON parser to do the parsing for you.Java Code:string json = in.readLine(); int index_of = json.indexOf("n\:"); // if you need it System.out.println("Sentence: " + json);
Final suggestion: put your code between [code][/code] tags in this forum. Looks much better.
Good luck.
ErikI'm new to Java but I like to help where ever I can. :)
- 11-12-2010, 12:54 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 14
- Rep Power
- 0
Thanks man,
I can finally output stuff, but now I get the EXACT same stuff from the notepad; example
The Universal Symbol Of The Catch.\r\u003Cbr/\u003E\r\u003Cbr/\u003EUsed extensively in web advertising, promotions, and spam. Placing the symbol after a statement is a legally permissible way to lie...
See how its got that junk there, some times it has it, sometimes it dosent..... is it because I dont have a proper json parser, and if not where do I download it.
thanks again man.
- 11-12-2010, 01:03 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
How are you reading/displaying that text?
- 11-12-2010, 01:39 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Hi,
Have a look at json.org. They came up with json in the first place :) and they have java classes to work with json. I played around with it just yet and the JSONObject class accepts the resultstring from your code perfectly.
Good luck.
ErikI'm new to Java but I like to help where ever I can. :)
- 11-12-2010, 02:10 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 14
- Rep Power
- 0
Either its right infront of my eye or I'm blind, cause I can't see where to download it, could you please show me :)
- 11-12-2010, 02:14 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
This is the Java directory at JSON.
There's a link to the zip.
It's not the easiest link to find on the front page I have to agree...:)
ETA: It's source code, so you need to include it as part of your project.
I thought there was a jar around somewhere.
- 11-12-2010, 02:46 PM #8
Member
- Join Date
- Nov 2010
- Posts
- 14
- Rep Power
- 0
I have no idea how to use this stuff lol:
:/Java Code:String json = in.readLine().toString(); JSONObject jj = new JSONObject(); int index_of = jj.toString().indexOf("n\":"); try { System.out.println("sentence man: " + jj.getString(json)); } catch (JSONException ex) { Logger.getLogger(myBot.class.getName()).log(Level.SEVERE, null, ex); }
output
and the contents of the jsonJava Code:SEVERE: null org.json.JSONException: JSONObject["
- 11-12-2010, 03:00 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
The whole String you have receieved is a JSON string.
Don't substring it.
Use the JSONObject constructor to create a JSONObject from that String. Then I think (you may have to play around with it a bit) you can use getString() to get the data you want based on the key (the bit before the colon).
ETA: Actually you may have to use getObject() initially. For example, assume your JSON string is something like:
{"head" : {"subobject" : {"thedataIwant" : "blah blah blah"}}}
to get to thedataIwant I woudl write:
At least that's the impression I get. Note this is a very basic example, and I have no doubt there are complications I haven't dealt with here.Java Code:JSONObject j = new JSONObject(myString); String thedata = j.getObject("head").getObject("subobject").getString("thedataIwant");Last edited by Tolls; 11-12-2010 at 03:04 PM.
Similar Threads
-
JSON Lib: json-lib-1.0-jdk13.jar
By Raghuraman K in forum Advanced JavaReplies: 3Last Post: 05-13-2010, 10:00 AM -
Print Variable From One Class to Output on another.
By Lyricid in forum New To JavaReplies: 8Last Post: 02-27-2010, 01:56 PM -
variable output is always 0
By 1Sserdda in forum New To JavaReplies: 2Last Post: 09-25-2009, 06:26 AM -
Your variable is never read
By singularity in forum New To JavaReplies: 4Last Post: 09-10-2009, 12:13 PM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks