Results 1 to 3 of 3
  1. #1
    slider57 is offline Member
    Join Date
    Aug 2011
    Posts
    29
    Rep Power
    0

    Default Using JSON object in java

    to preface, I hope I am posting this in the right spot.

    I have a JSON Object and am looking for specific information within it.

    {
    "parliament": 35,
    "statements": [
    {
    "attribution": "The Speaker",
    "url": "/debates/1996/2/28/the-speaker-1/",
    "text": "As is our custom, we will now sing O Canada, which will be led by the hon. member for Vancouver East.",
    "topic": "",
    "time": "1996-02-28T14:00:00",
    "heading": ""
    },
    {
    "politician": {
    "name": "Sarkis Assadourian",
    "url": "/politicians/4425/",
    "member_id": 3164,
    "party": "Liberal",
    "riding": "Don Valley North (Ontario)",
    "id": 4425
    },
    "url": "/debates/1996/2/28/sarkis-assadourian-1/",
    "text": "Mr. Speaker, on February 14, 1996 Pleasant View Junior High School in my riding of Don Valley North held a special Canadian citizenship week to celebrate National Citizenship Week and mark the 25th anniversary of the school.\n\nI salute Pleasant View Junior High on its silver anniversary and commend the principal, staff and students in choosing the symbol of reaffirmation of their Canadian citizenship as a meaningful way to celebrate this significant milestone in Pleasant View's history.\n\nCongratulations Pleasant View junior high school and happy 25th anniversary.",
    "topic": "Pleasant View Junior High School",
    "time": "1996-02-28T14:00:00",
    "attribution": "Mr. Sarkis Assadourian (Don Valley North, Lib.)",
    "heading": "Statements By Members"
    },
    {
    "politician": {
    "name": "Charlie Penson",
    "url": "/politicians/3017/",
    "member_id": 3068,
    "party": "Reform",
    "riding": "Peace River (Alberta)",
    "id": 3017
    },

    etc....

    That is the contents of the object, my question is how do I extract the information under politician. I have tried using the JSONObject.getString("politician") method but it cant seem to see politician. I assume its a nesting problem... i guess. Anyway, any help would be appreciated. Thanks.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,269
    Rep Power
    6

    Default Re: Using JSON object in java

    What you will probably want to do is to translate that heap of JSON data into a Java object hierarchy. That way only during data loading do you need to be aware that there is such a thing as JSON, after the loading you just have your Plain Old Java Objects and you can use their getters to retrieve the information you need.

    I don't know what you are using, but both the Google Gson API and Jackson can map JSON data to Java objects.

    google-gson - A Java library to convert JSON to Java objects and vice-versa - Google Project Hosting

    JacksonInFiveMinutes - FasterXML Wiki

  3. #3
    slider57 is offline Member
    Join Date
    Aug 2011
    Posts
    29
    Rep Power
    0

    Default Re: Using JSON object in java

    Thanks for your response. That sounds good I will go ahead and look into that.

Similar Threads

  1. could not convert the xml to json using java
    By cgk_js in forum Advanced Java
    Replies: 0
    Last Post: 11-30-2012, 10:31 AM
  2. Java/Android JSON Problem
    By fresh83 in forum New To Java
    Replies: 1
    Last Post: 11-03-2011, 08:45 PM
  3. JSON Lib: json-lib-1.0-jdk13.jar
    By Raghuraman K in forum Advanced Java
    Replies: 3
    Last Post: 05-13-2010, 11:00 AM
  4. JSON from javascript to java servlet
    By boss-tech in forum Java Servlet
    Replies: 1
    Last Post: 09-18-2009, 09:27 AM

Tags for this Thread

Posting Permissions

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