Results 1 to 9 of 9
  1. #1
    Jman85 is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

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

  2. #2
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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:

    Java Code:
    string json = in.readLine();
    int index_of = json.indexOf("n\:"); // if you need it
    System.out.println("Sentence: " + json);
    Another suggestion: google for a JSON parser to do the parsing for you.

    Final suggestion: put your code between [code][/code] tags in this forum. Looks much better.

    Good luck.
    Erik
    I'm new to Java but I like to help where ever I can. :)

  3. #3
    Jman85 is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default

    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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    How are you reading/displaying that text?

  5. #5
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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.
    Erik
    I'm new to Java but I like to help where ever I can. :)

  6. #6
    Jman85 is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default

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

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    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.

  8. #8
    Jman85 is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default

    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

    Java Code:
    SEVERE: null
    org.json.JSONException: JSONObject["
    and the contents of the json

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    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:
    Java Code:
    JSONObject j = new JSONObject(myString);
    String thedata = j.getObject("head").getObject("subobject").getString("thedataIwant");
    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.
    Last edited by Tolls; 11-12-2010 at 03:04 PM.

Similar Threads

  1. JSON Lib: json-lib-1.0-jdk13.jar
    By Raghuraman K in forum Advanced Java
    Replies: 3
    Last Post: 05-13-2010, 10:00 AM
  2. Print Variable From One Class to Output on another.
    By Lyricid in forum New To Java
    Replies: 8
    Last Post: 02-27-2010, 01:56 PM
  3. variable output is always 0
    By 1Sserdda in forum New To Java
    Replies: 2
    Last Post: 09-25-2009, 06:26 AM
  4. Your variable is never read
    By singularity in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 12:13 PM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •