Decoding JSON object in JAVA

  • #1
117
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.
 

Answers and Replies

  • #2
117
3
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?
 
  • #3
117
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:
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:
  • #4
191
3
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).
 
  • #5
117
3
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
 
  • #6
117
3
I don't know how to mark as solved, but this issue has been resolved...
 

Related Threads on Decoding JSON object in JAVA

Replies
4
Views
949
Replies
4
Views
3K
  • Last Post
Replies
19
Views
2K
  • Last Post
Replies
1
Views
7K
  • Last Post
Replies
1
Views
4K
Replies
8
Views
994
  • Last Post
Replies
8
Views
2K
  • Last Post
Replies
9
Views
2K
  • Last Post
Replies
6
Views
2K
Top