-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);
6
  • 1
    What you tried so far ? Commented Jun 15, 2017 at 8:34
  • 1
    What exactly is your question? What have you tried? Furthermore, that is not valid JSON. Commented Jun 15, 2017 at 8:34
  • sir i create data format like this . . and post to database . . help me Commented Jun 15, 2017 at 8:40
  • 2
    Possible duplicate of How to create Json using JsonArray and JsonObject Commented Jun 15, 2017 at 8:41
  • so tried my best once you show, what you've tried, we'll be able to help. You can't just ask to give you full working code. Commented Jun 15, 2017 at 8:53

2 Answers 2

0
JSONObject jObj = new JSONObject();
jObj.put(KEY,VALUE);
JSONArray jArray = new JsonArray();
jArray.put(jObj);
JSONObject mainJson = new JSONObject();
mainJson.put(KEY,mainJson);
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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!!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.