Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JSON reader

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Where
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JSON reader

    I've done a search on these forums and exhausted my link supplies on google so it's time to ask my question here.


    Quite simply I've got an application that connects to a website and gathers the JSON string but now I can't figure out how to parse the information.... it seemed to much easier in php.

    I've tried using some of the json libraries available, such as gson and other various ones found at json.org, but simply can't figure it out as they all seem to over complicate what I'm trying to do.


    So does anyone have an explanation on how I can do this without using an API that simply over complicates it or perhaps a source that will explain things a little more clearly for me to be able to use this information?



    Thanks in advance.


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Where
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JSON reader

    awww no one


    Guess I'll get cracking on it again this morning.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Where
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JSON reader

    After digging through a ton of stuff this morning I finally came across the answer I was looking for. Hopefully this will help someone else out that does a search on the forums with the same problem:

    Solution:

    You've got to use one of the many APIs out there, for my simple task of just reading the string I chose to use JSON-Simple. After that the codes pretty simple it just took me a really long time to find out how simple it was as the documentation is overly complicated for the task... here's the link I followed: LINK.


    I hope this helps make it easier on anyone else that ran into the problem.

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Where
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JSON reader

    OK, ran into another problem.

    Keep getting an error when I try to read an array in the object... Here's the chunk of code that's giving me the problem:

    			Object obj = parser.parse(json);
    			JSONObject jsonObject = (JSONObject) obj;
     
    			JSONArray msg = (JSONArray) jsonObject.get("guild");
    			Iterator<String> iterator = msg.iterator();
    			while (iterator.hasNext()) {
    				System.out.println(iterator.next());
    			}


    And here's the error message:

    java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray


    I understand what the error message is saying I just can't seem to figure out a solution to get it to work.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Where
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JSON reader

    Simple answer: it wasn't an array in the json feed it was another object... all you had to do was make another object.

Similar Threads

  1. Save JSON obj via Jfilechooser
    By nautilusz in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 24th, 2012, 06:15 AM
  2. PHP Developer - OOP / MVC / XML / JSON - £40,000 (NEG)
    By JacquelineRobbins in forum Paid Java Projects
    Replies: 0
    Last Post: January 12th, 2012, 06:58 AM
  3. Parsing JSON Feed
    By sogorman in forum Other Programming Languages
    Replies: 2
    Last Post: June 6th, 2011, 03:02 AM
  4. Validate JSON according XSD schema
    By edrozim in forum Java Theory & Questions
    Replies: 2
    Last Post: February 23rd, 2011, 10:42 AM
  5. Replies: 5
    Last Post: October 12th, 2009, 12:24 AM