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

  • Thread starter Thread starter iamjon.smith
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The discussion revolves around decoding a JSON object returned to a .jsp page, specifically focusing on the "columnGridContainer" key, which contains a string representation of an array. The user is seeking a method to convert the entire JSON object into a multidimensional array for easier manipulation. They have confirmed the JSON is valid but are struggling with parsing it effectively. The user is utilizing the org.json library and has attempted to manually extract values but prefers a looping method to populate an array with key-value pairs. Suggestions include writing a custom parser to handle the string or using existing methods to convert the JSON object into an array format. The conversation indicates a need for clarity in parsing techniques and the importance of handling the specific string format within "columnGridContainer." The issue has been resolved, indicating that the user found a solution to their parsing challenge.
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...
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

Similar threads

Back
Top