我想创建jsonarray,但我是新来的,所以尽我所能,但无法做到这一点,任何人都可以帮助我。谢谢我已经知道我从这个数组获取数据,但现在我想以这种格式的json数组插入我的文本以发布到数据库
{
"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":
}
我现在的代码,我只能制作5个第一个obj,但之后还有更多的ServiceTasks数组,所以我对此感到困惑
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);
答案 0 :(得分:0)
JSONObject jObj = new JSONObject();
jObj.put(KEY,VALUE);
JSONArray jArray = new JsonArray();
jArray.put(jObj);
JSONObject mainJson = new JSONObject();
mainJson.put(KEY,mainJson);
答案 1 :(得分: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);
//**
我想这是你想要建立的json。 希望有所帮助!!!