Reputation: 3770
I have a json array which i am putting in a map.
for(int i = 0; i < jsonarr.length(); i++){
try {
JSONObject jsonobj = jsonarr.getJSONObject(i);
Iterator<Object> keysItr = jsonobj.keys();
while(keysItr.hasNext()) {
Object key = keysItr.next();
Object value = jsonobj.get((String) key);
map.put((String) key, value);
}
System.out.println(map);
But when I display the map it gives me a string in which there is one curly brace missing.
Output
{
subtype=text,
maxlength=22,
values=[
{
"label":"Time",
"value":"two",
"selected":true
},{
"label":"Milk",
"value":"hot"
},{
"label":"sky",
"value":"blue"
}
],
(HERE)
name=text-1496816623336,
description=sa,
className=form-control,
label=Text Field,
type=text,
required=true
}
I have used jackson and gson also but in that case they weren't even putting the whole json into map.It might be a stupid question but thanks.
EDIT :
Here is the JSON used to create the Map
[
{
"type":"select",
"label":"Select",
"className":"form-control",
"name":"select-1496823185891",
"values":[
{
"label":"Option 1",
"value":"option-1",
"selected":true
},{
"label":"Option 2",
"value":"option-2"
},{
"label":"Option 3",
"value":"option-3"
}
]
},{
"type":"text",
"label":"Text Field",
"className":"form-control",
"name":"text-1496823186970",
"subtype":"text"
}
]
Upvotes: 0
Views: 887
Reputation: 1361
Hey nope there is no curly brace missing,.... format the output and you will see:
{
subtype=text, maxlength=22, values=[
{"label":"Time","value":"two","selected":true},
{"label":"Milk","value":"hot"},
{"label":"sky","value":"blue"}
],(HERE)
name=text-1496816623336,
description=sa,
className=form-control,
label=Text Field,
type=text,
required=true
}
Edit (answer to your comment): This is a valid Json string:
[{"type":"select","label":"select","className":"form-control","name":"select-1496823185891","values":[{"label":"Option 1","value":"option-1","selected":true},{"label":"Option 2","value":"option-2"},{"label":"Option 3","value":"option-3"}]},{"type":"text","label":"Text Field","className":"form-control","name":"text-1496823186970","subtype":"text"}]
.
Formated:
[
{
"type": "select",
"label": "select",
"className": "form-control",
"name": "select-1496823185891",
"values": [
{
"label": "Option 1",
"value": "option-1",
"selected": true
},
{
"label": "Option 2",
"value": "option-2"
},
{
"label": "Option 3",
"value": "option-3"
}
]
},
{
"type": "text",
"label": "Text Field",
"className": "form-control",
"name": "text-1496823186970",
"subtype": "text"
}
]
In your one there is some hidden chars between "values"
and the :
.
Upvotes: 1
Reputation: 14572
If what I thing is that you expect to still have two JsonObject
in the Map
. The problem is that you iterate the JsonArray
to put every key/value in the same Map
, that means you override the previous value with the last one.
--first item :
map.put("type","select");
--seconds item:
map.put("type","text");
-- result
map.get("type"); //"text"
A Map
is flat, you can't have two values for two keys, you could create a collection of Map
to store every value like a List<Map>
, on each JsonObject
to iterate, you create a new Map
that you store in the list
Upvotes: 0