-4

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?

blsn
  • 1,077
  • 3
  • 18
  • 38
  • 1
    Does this answer your question? [Why does my ArrayList contain N copies of the last item added to the list?](https://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list) – Savior May 26 '20 at 17:56
  • JSONArray keys = metadataReq.names(); this is not clear, how does this value looks like? – Pakira May 26 '20 at 18:08
  • It says to fix that, I shall move the object construction inside the loop .. but I can't see how it will be done in my case – blsn May 26 '20 at 18:08
  • As for metadataReq, see my code, 'String key' and 'String val', fetched from it – blsn May 26 '20 at 18:11
  • for better understanding you should add metadataReq JSONObject, does it look like this: JSONObject metadataReq = new JSONObject(); metadataReq.put("value","Green"); metadataReq.put("value","Nylon"); – Pakira May 26 '20 at 18:20

2 Answers2

1

The problem in the above code is JSONObject obj = new JSONObject(); initialised only one but you need to create inside the loop. This code gives the expected output as you want:

public static void main(String[] args) throws JSONException {

    JSONObject index = new JSONObject();
    JSONObject jsonRecipe = new JSONObject();


    JSONObject metadataReq = new JSONObject();
    metadataReq.put("technology","Ceramics");
    metadataReq.put("material","Nylon");
    metadataReq.put("color","Green");


    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);

       JSONObject obj = new JSONObject();
       obj.put("attribute", key);
       obj.put("value", val);
       obj.put("mandatory", "rule");

       index.put(j, obj);
       jsonRecipe.put("metadata", index);
    }

    System.out.println(jsonRecipe);

}
Pakira
  • 1,951
  • 3
  • 25
  • 54
0

You only created 1 object and inserted it 3 times, as opposed to creating 3 distinct objects.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • That's what I'm supposed to do, create once and loop through 3 times with different values – blsn May 26 '20 at 18:04
  • 1
    One object can't have 3 different values *at the same time*, but 3 different objects can each have its own value. – Scott Hunter May 26 '20 at 18:31