How do I iterate and enter values from a mapped entrySet into an array?

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

The discussion focuses on iterating through a mapped entrySet from a JsonObject in Java and extracting values into an array. The provided code snippet demonstrates how to access the entrySet using the `Set>` structure. The user is advised to print each key-value pair individually instead of the entire set to better understand the data structure. The corrected approach involves using `singleItem.getKey()` and `singleItem.getValue()` within the loop to achieve the desired output.

PREREQUISITES
  • Java programming language knowledge
  • Understanding of JSON data structures
  • Familiarity with Java Collections Framework
  • Experience with the Gson library for JSON manipulation
NEXT STEPS
  • Learn how to convert a JsonElement to a Java array using Gson
  • Explore Java's `ArrayList` for dynamic array handling
  • Investigate advanced JSON parsing techniques with Gson
  • Study Java's enhanced for-loop for iterating over collections
USEFUL FOR

Java developers, software engineers working with JSON data, and anyone looking to manipulate and iterate over JSON objects in their applications.

iamjon.smith
Messages
117
Reaction score
3
Using the following code:

Code:
Set<Map.Entry<String,JsonElement>> mySet = jsonObject.entrySet();
for(Map.Entry<String,JsonElement> singleItem : mySet)
{
	singleItem.getKey();
	System.out.println("Set values: " + mySet);
}

mySet gets printed to the console as:

Code:
Set values: [columnGridContainer="[[1,\"1\",\"Default Value\",\"Y\",\"N\",\"0\",\"Left\",\"Default Value\",\"Default Value\",\"Default Value\",\"Default Value\",\"Left\",\"Center\",\"Center\",\"Center\",\"#000056\",\"None\",\"Date/Time\",\"No Filter\",\"N\"]]", header_row_2_enabled="Y", header_row_3_enabled="Y", header_row_4_enabled="Y", col_widths_units="pixels", default_sub_rows_closed="Y", extjs_component="grid", refreshrate="181", title="everything works right", subtitle="except display for checkboxes...", autoheight="N", bottom_buttons="N", col_label_source="col_config_table"]

I need to iterate through the values and put them into an array. Please provide some direction as to how to iterate over this mapped entrySet and enter each value into an array.
 
Technology news on Phys.org
You are printing out the whole set in your for loop, don't you want to look at each item? Like this:

Code:
Set<Map.Entry<String,JsonElement>> mySet = jsonObject.entrySet();
System.out.println("mySet as 'key : value'");
for(Map.Entry<String,JsonElement> singleItem : mySet)
{
	System.out.println(singleItem.getKey() + " : " + singleItem.getValue());
}
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K