0

Here is json:

{
  "infos":[{
      "targetId":1,
      "celsius":34.5,
      "measuredTime":"2021-03-25,11:40:01", 
      "isFirst":true }]
 }

I am try to put JSONArray to JSONObject:

     JSONObject object = new JSONObject();
     object.put("infos" , array);

I don't know how to create a Array? please help me , thanks.

林姿妤
  • 43
  • 6

1 Answers1

0
        JSONObject object = new JSONObject();
        try {
            // create multiple object
            JSONObject obj1 = new JSONObject();
            obj1.put("targetId", 1);
            obj1.put("celsius", 34.5);
            obj1.put("measuredTime", "2021-03-25,11:40:01");
            obj1.put("isFirst", true);

            // add all created object in array
            JSONArray array = new JSONArray();
            array.put(obj1);

            // add array in main object
            object.put("infos" , array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
Nilesh B
  • 937
  • 1
  • 9
  • 14