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

  • Thread starter Thread starter iamjon.smith
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The discussion centers on iterating through a `Set<Map.Entry<String, JsonElement>>` derived from a JSON object in Java. The initial code snippet prints the entire set of entries, which is not the intended outcome. Instead, the focus should be on accessing individual key-value pairs. The suggested approach involves modifying the for loop to print each entry in the format "key : value" using `singleItem.getKey()` and `singleItem.getValue()`. Additionally, to store the values in an array, one can create an array of appropriate size and populate it within the loop by accessing `singleItem.getValue()`. This ensures that each value from the JSON object is captured and stored correctly.
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());
}
 
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...

Similar threads

Replies
5
Views
3K
Back
Top