What is the Best Way to Decode a JSON Object in Java?

  • Context: Java 
  • Thread starter Thread starter iamjon.smith
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around decoding a JSON object in Java, specifically focusing on how to extract key-value pairs into an array format. Participants explore various methods and libraries for parsing JSON, including Gson and org.json, while addressing challenges related to the structure of the JSON data.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a JSON object and expresses difficulty in decoding it into an array, particularly due to the string format of the "columnGridContainer" value.
  • Another participant clarifies that the JSON object is valid and suggests the need to first convert the object into an array before manipulating the string values.
  • A different participant shares a code snippet using Gson to parse the JSON but questions the availability of a toArray() method within the library.
  • One participant articulates a specific structure they desire for the resulting array, indicating confusion about how to achieve a multidimensional array from the JSON object.
  • Another participant mentions that they are using org.json instead of Gson and seeks to continue with that library for parsing.
  • A later reply indicates that the issue has been resolved, though no details are provided on the solution.

Areas of Agreement / Disagreement

Participants express varying approaches to parsing the JSON object, with no consensus on a single method or solution. Some prefer using Gson while others are focused on org.json, indicating a lack of agreement on the best approach.

Contextual Notes

Participants discuss the limitations of their current understanding and the specific requirements for the desired array structure, highlighting the complexity of parsing nested JSON strings.

iamjon.smith
Messages
117
Reaction score
3
I have a JSON Object being returned to a .jsp page. the object comes back:

Code:
{"columnGridContainer":"[[1,\"1\",\"bridge_routine_link\",\"Y\",\"Y\",\"100\",\"CENTER\",\"Cheese\",\"\",\"\",\"\",\"LEFT\",\"LEFT\",\"LEFT\",\"LEFT\",\"\",\"0\",\"string\",\"string\",\"\"]]","header_row_2_enabled":"N","header_row_3_enabled":"N","header_row_4_enabled":"N","col_widths_units":null,"default_sub_rows_closed":"N","extjs_component":null,"refreshrate":"","title":"","subtitle":"","autoheight":"Y","bottom_buttons":"Y","col_label_source":null}

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.
 
Technology news on Phys.org
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?
 
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:
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:
I made it a bit more readable:

Code:
{"columnGridContainer":"[[1,\"1\",\"bridge_routine_link\",\"Y\",\"Y\",\"100\",\"CENTER\",\"Cheese\",\"\",\"\",\"\",\"LEFT\",\"LEFT\",\"LEFT\",\"LEFT\",\"\",\"0\",\"string\",\"string\",\"\"]]",
"header_row_2_enabled":"N",
"header_row_3_enabled":"N",
"header_row_4_enabled":"N",
"col_widths_units":null,
"default_sub_rows_closed":"N",
"extjs_component":null,
"refreshrate":"",
"title":"",
"subtitle":"",
"autoheight":"Y",
"bottom_buttons":"Y",
"col_label_source":null}

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).
 
Apparently I am still not being clear enough. I need an array that contains:

Code:
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
 
I don't know how to mark as solved, but this issue has been resolved...
 

Similar threads

Replies
1
Views
5K