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());
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
5
Views
3K
Back
Top