-3

i want to create jsonarray but i am new here so tried my best but unable to do that can anybody help me in that. Thanks i already know i to get data from this array but now i want to insert my text in this format of json array to post to database

{
  "ServiceVisitID": 1,
  "ServiceItemID": 2,
  "ItemDescription": "sample string 3",
  "SerialNumber": "sample string 4",
  "ItemLocation": "sample string 5",
  "ServiceTasks": [
    {
      "TaskID": 1,
      "Task": "sample string 2",
      "Description": "sample string 3",
      "done": true,
      "Notes": "sample string 5",
      "ReasonID": 6,
      "ResultTypeID": 7
    },
    {
      "TaskID": 1,
      "Task": "sample string 2",
      "Description": "sample string 3",
      "done": true,
      "Notes": "sample string 5",
      "ReasonID": 6,
      "ResultTypeID": 7
    }
  ],
  "ItemAttended": true,
  "NotAttendedReasonCode": "sample string 7",
  "ActionRequiredID": 8,
  "ItemsRequired": [
    {
      "ItemID": 1,
      "StockCode": "sample string 2",
      "Description": "sample string 3",
      "Quantity": 4
    },
    {
      "ItemID": 1,
      "StockCode": "sample string 2",
      "Description": "sample string 3",
      "Quantity": 4
    }
  ],
  "FurtherVisitRequired": true,
  "Notes": "sample string 10",
  "ServiceComplete": true,
  "Code":
  "Message": 
}​

my present code that i only able to make just 5 first obj but after that there is further array of ServiceTasks so i am confused in this

 JSONArray jsonArray=new JSONArray();
                                    JSONObject ob=new JSONObject();

                                   ob.put("ServiceVisitID","1");
                                    ob.put("ServiceItemID","1");
                                    ob.put("ItemDescription","check");
                                    ob.put("SerialNumber","check");
                                    JSONObject itemlocationob=  ob.put("SerialNumber");
                                   ob.put("ItemLocation","check");

                                    JSONArray ServiceTasks=new JSONArray();
                                    JSONObject ServiceTasksob=new JSONObject();

                                    ServiceTasksob.put("TaskID","1");
                                    ServiceTasksob.put("TaskID","1");
                                    ServiceTasksob.put("TaskID","1");
                                    ServiceTasks.put(jsonObject1);
                                    ob.put("",ServiceTasks);
Media
  • 13
  • 5

2 Answers2

0
JSONObject jObj = new JSONObject();
jObj.put(KEY,VALUE);
JSONArray jArray = new JsonArray();
jArray.put(jObj);
JSONObject mainJson = new JSONObject();
mainJson.put(KEY,mainJson);
Anand Jain
  • 2,365
  • 7
  • 40
  • 66
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Badacadabra Jun 15 '17 at 15:01
0
    In an JSONObject we can follow as key value pairs
Considering your code,
JSONArray jsonArray=new JSONArray();
                                    JSONObject ob=new JSONObject();

                                   ob.put("ServiceVisitID","1");
                                    ob.put("ServiceItemID","1");
                                    ob.put("ItemDescription","check");
                                    ob.put("SerialNumber","check");
                                    **//  Wrong version
                                    JSONObject itemlocationob=  ob.put("SerialNumber");  
                                    ob.put("ItemLocation","check");**
                                    //
                                    **//Right version
                                    JSONObject itemlocationob=  new JSONObject();
                                    itemlocationob.put("SerialNumber","<some value>");
                                   ob.put("ItemLocation",itemlocationob);

                                    JSONArray ServiceTasks=new JSONArray();
                                    JSONObject ServiceTasksob=new JSONObject();

                                    ServiceTasks.put("TaskID","1");
                                    ServiceTasks.put("TaskID","2");
                                    ServiceTasks.put("TaskID","3");
                                                                                          ServiceTasksob.put("ServiceTasks",ServiceTasks);
                                    ob.put("service",ServiceTasksob);
                                   //**

I guess this kind of json you are trying to build. Hope that helps!!!

priya raj
  • 362
  • 2
  • 8