Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Decoding JSON object in JAVA

  1. Feb 24, 2014 #1
    I have a JSON Object being returned to a .jsp page. the object comes back:

    Code (Text):
    I can't figure out for the life of me how to decode this into an array. I have used JSONLint to determine that it is a valid JSON object, but the encoding in the "columnGridContainer" is breaking everything I try. Any advice would be greatly appreciated.
  2. jcsd
  3. Feb 24, 2014 #2
    The Returnd data is a valid JSON OBJECT here. The value of the key: "columnGridContainer" in the key:value pair is a string. However, I need all the key:pairs in an array first. At that point, I would like to take the first item in the new array (columnGridContainer) and manipulate that string value to get the individual values out of the string. How can I decode this object into an array first?
  4. Feb 24, 2014 #3
    Ok, let me clear this up a little (if I can)...

    The object I am getting back is fine. I just need to be able to loop through the object and get the name value pairs out of the object and into an array. I can manually remove each value like:

    Code (Text):
    String jsonArrayForColumnsAsString = jsonObject.get("columnGridContainer").getAsString();
         JsonArray myColumns = jsonParser.parse(jsonArrayForColumnsAsString).getAsJsonArray();
         JsonArray dataArray = jsonObject.getAsJsonArray("columnGridContainer");
    but would rather do it with a loop and then manipulate the array as needed.
    Last edited: Feb 24, 2014
  5. Feb 25, 2014 #4
    I made it a bit more readable:

    Code (Text):
    I'm using (Google's) Gson library for JSON parsing on my hobby site, and it's easy to use. I can't think offhand if it has a toArray() method though.

    Are you wanting to parse it manually? You could write your own parser, but it's a bit of work. Just read through the string and split on :," delimiters (and handle \" differently - made a sub-parser, or just leave it as a string and handle it later).
  6. Feb 25, 2014 #5
    Apparently I am still not being clear enough. I need an array that contains:

    Code (Text):
    myArray[0][0] = ["columnGridContainer"]["long string of data to dealt with later"]
    myArray[1][1] = ["header_row_2_enabled"]["N"]
    myArray[2][2] = ["header_row_3_enabled"]["N"]
    myArray[3][3] = ["header_row_4_enabled"]["N"]
    myArray[4][4] = ["col_widths_units"][null]
    myArray[5][5] = ["default_sub_rows_closed"]["N"]...etc.,etc.
    I have apparently forgotten how to code and for the life of me can't figure out how to parse the perfectly usable json object that is being returned into a ??multidimensional?? array.

    I am using org.json to get to this point (NO GSON) and need to continue doing so
  7. Feb 26, 2014 #6
    I don't know how to mark as solved, but this issue has been resolved...
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook