Loading ...
Sorry, an error occurred while loading the content.

Parsing JSON

Expand Messages
  • Frans Thamura @ flatburger
    hi all i want to parse the book.json and i want to make sure that the Books class have Book , and every Book class have value. i try using Java, and use the
    Message 1 of 2 , Apr 23 2:07 AM
    • 0 Attachment
      hi all

      i want to parse the book.json and i want to make sure that the Books
      class have Book , and every Book class have value.

      i try using Java, and use the JSON Java

      but stil cannot get the value

      right now i still cannot get the values of the books, which i know have
      3 book there.


      this is my syntax, still error


      JSONArray jsonObject = JSONArray.fromString(responseBody);
      jsonObject.fromString("books");
      System.out.println(jsonObject.get(0));

      ----------

      [{"books":[{"book":
      {
      "title":"JavaScript, the Definitive Guide",
      "publisher":"O'Reilly",
      "author":"David Flanagan",
      "cover":"/images/cover_defguide.jpg",
      "blurb":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
      }
      },
      {"book":
      {
      "title":"DOM Scripting",
      "publisher":"Friends of Ed",
      "author":"Jeremy Keith",
      "cover":"/images/cover_domscripting.jpg",
      "blurb":"Praesent et diam a ligula facilisis venenatis."
      }
      },
      {"book":
      {
      "title":"DHTML Utopia: Modern Web Design using JavaScript & DOM",
      "publisher":"Sitepoint",
      "author":"Stuart Langridge",
      "cover":"/images/cover_utopia.jpg",
      "blurb":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
      }
      }
      ]}]

      [Non-text portions of this message have been removed]
    • Kevin Smith
      Sorry I didn t reply sooner. Hopefully you have already solved the problem. If not... I think you would want code more like this (untested!): JSONArray
      Message 2 of 2 , May 13, 2007
      • 0 Attachment
        Sorry I didn't reply sooner. Hopefully you have already solved the
        problem. If not...

        I think you would want code more like this (untested!):

        JSONArray outerBooksArray = JSONArray.fromString(responseBody);
        JSONObject booksObject = outerBooksArray.get(0);
        JSONArray innerBooksArray = booksObject.getObject("books");

        JSONObject book = innerBooksArray.get(0);
        JSONObject bookDetails = book.getObject("book");
        System.out.println("title: " + bookDetails.getString("title"));


        The code would be much simpler if your data had fewer layers. Perhaps
        as simple as an array containing bookdetails jsonobjects.

        Kevin


        --- In json@yahoogroups.com, "Frans Thamura @ flatburger"
        <flatburger@...> wrote:
        >
        > hi all
        >
        > i want to parse the book.json and i want to make sure that the Books
        > class have Book , and every Book class have value.
        >
        > i try using Java, and use the JSON Java
        >
        > but stil cannot get the value
        >
        > right now i still cannot get the values of the books, which i know have
        > 3 book there.
        >
        >
        > this is my syntax, still error
        >
        >
        > JSONArray jsonObject = JSONArray.fromString(responseBody);
        > jsonObject.fromString("books");
        > System.out.println(jsonObject.get(0));
        >
        > ----------
        >
        > [{"books":[{"book":
        > {
        > "title":"JavaScript, the Definitive Guide",
        > "publisher":"O'Reilly",
        > "author":"David Flanagan",
        > "cover":"/images/cover_defguide.jpg",
        > "blurb":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
        > }
        > },
        > {"book":
        > {
        > "title":"DOM Scripting",
        > "publisher":"Friends of Ed",
        > "author":"Jeremy Keith",
        > "cover":"/images/cover_domscripting.jpg",
        > "blurb":"Praesent et diam a ligula facilisis venenatis."
        > }
        > },
        > {"book":
        > {
        > "title":"DHTML Utopia: Modern Web Design using JavaScript & DOM",
        > "publisher":"Sitepoint",
        > "author":"Stuart Langridge",
        > "cover":"/images/cover_utopia.jpg",
        > "blurb":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
        > }
        > }
        > ]}]
        >
        > [Non-text portions of this message have been removed]
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.