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

  • Java
  • Thread starter iamjon.smith
  • Start date
  • Tags
    Java
In summary, the data being returned to a .jsp page is a valid JSON object, but I need to be able to loop through the object and get the name value pairs out of the object and into an array.
  • #1
iamjon.smith
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.
 
Technology news on Phys.org
  • #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?
 
  • #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
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
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
I don't know how to mark as solved, but this issue has been resolved...
 

1. What is a JSON object in Java?

A JSON object in Java is a data structure that stores information in a specific format. It is used to represent data in a human-readable and machine-readable way, making it easier to transfer and parse data between different systems.

2. How do you decode a JSON object in Java?

To decode a JSON object in Java, you can use a library such as GSON or Jackson that provides methods for parsing and converting JSON data into Java objects. These libraries handle the conversion of JSON data into Java objects, making it easier for developers to work with JSON data in their Java code.

3. What is the difference between decoding and encoding a JSON object in Java?

Decoding a JSON object in Java involves converting JSON data into Java objects, while encoding involves converting Java objects into JSON data. Decoding is used when retrieving data from an external source, while encoding is used when sending data to an external source.

4. How do you handle errors when decoding a JSON object in Java?

When decoding a JSON object in Java, it is important to handle errors that may occur during the decoding process. This can be done by using exception handling techniques, such as try-catch blocks, to catch any errors that may occur and handle them appropriately.

5. Can you decode a nested JSON object in Java?

Yes, it is possible to decode a nested JSON object in Java. This can be done by using the appropriate methods provided by the JSON parsing library. The nested data will be converted into nested Java objects, making it easier to access and manipulate the data within the JSON object.

Similar threads

  • Programming and Computer Science
Replies
1
Views
4K
Back
Top