I try to create the following JSON:
{
"metadata": {
"0": {
"attribute": "technology",
"value": "Ceramics",
"mandatory": "rule"
},
"1": {
"attribute": "color",
"value": "Green",
"mandatory": "rule"
},
"2": {
"attribute": "material",
"value": "Nylon",
"mandatory": "rule"
}
}
}
metadataReq
is type JSONObject and it contains the values above
JSONObject obj = new JSONObject();
JSONObject index = new JSONObject();
JSONArray keys = metadataReq.names();
for (int i = 0; i < metadataReq.length (); i++) {
String key = keys.getString (i);
String val = metadataReq.getString (key);
String j = Integer.toString(i);
obj.put("attribute", key);
obj.put("value", val);
obj.put("mandatory", "rule");
index.put(j, obj);
jsonRecipe.put("metadata", index);
}
My script for some reason cause the last object (index #2), to show similar values on the other two objects (#0 and #1)
Using the for
loop, how to create it correctly?