我尝试创建包含多个JsonArray
和JsonObject
的Json字符串。我想创建Json字符串,格式如下所示:
[
{
"CustomerId": -1,
"PoolGuyId": 117,
"FilterChanges": [
{
"FilterChangeId": -1,
"CustomerId": -1,
"Reminders": [
{
"ReminderId": -1,
"FilterChangeId": -1,
"CustomerId": -1
},
{
"ReminderId": -2,
"FilterChangeId": -1,
"CustomerId": -1
}
]
},
{
"FilterChangeId": -2,
"CustomerId": -1,
"Reminders": [
{
"ReminderId": -3,
"FilterChangeId": -1,
"CustomerId": -1
}
]
}
],
"IsDeleted": false
}
]
我尝试了很多参考,但我的字符串仍未创建。 帮助我
我从数据库中获得价值。在sqlite数据库中有3个表,名为customerDetails,JobHistory,Reminder。在这些表中存储了多个值。
try {
customerDetailsTable = new CustomerDetailsTable(con);
customerDetailsTable.open();
arrayListCustomer = customerDetailsTable.getAllContacts();
customerDetailsTable.close();
JSONObject objCust = null;
JSONArray jsonArrayCust = new JSONArray();
for (int i = 0; i < arrayListCustomer.size(); i++) {
customerDetailsSetGet = arrayListCustomer.get(i);
objCust = new JSONObject();
try {
objCust.put("CustomerId", customerDetailsSetGet.getCustomerID());
objCust.put("PoolGuyId", customerDetailsSetGet.getPoolGuyId());
} catch (JSONException e) {
}
ArrayList<JobHistorySetGet> arrayListjobHistory = new ArrayList<>();
jobHistoryTable = new JobHistoryTable(con);
jobHistoryTable.open();
arrayListjobHistory = jobHistoryTable.getJobsAsPerCustId(customerDetailsSetGet.getCustomerID());
jobHistoryTable.close();
JSONObject objJobHis = null;
JSONArray jsonArrayJob = new JSONArray();
for (int j = 0; j < arrayListjobHistory.size(); j++) {
jobHistorySetGet = arrayListjobHistory.get(j);
objJobHis = new JSONObject();
try {
objJobHis.put("FilterChangeId", jobHistorySetGet.getFilterChangeId());
objJobHis.put("CustomerId", jobHistorySetGet.getCustomerId());
} catch (JSONException e) {
}
ArrayList<ReminderSetGet> arrayListReminder = new ArrayList<>();
reminderTable = new ReminderTable(con);
reminderTable.open();
arrayListReminder = reminderTable.getReminderAsPerJobID(jobHistorySetGet.getId());
reminderTable.close();
JSONArray jsonArrayReminder = new JSONArray();
JSONObject objReminder = null;
for (int k = 0; k < arrayListReminder.size(); k++) {
reminderSetGet = arrayListReminder.get(k);
objReminder = new JSONObject();
try {
Log.d("Anuj", "9");
objReminder.put("ReminderId", reminderSetGet.getReminderId());
objReminder.put("FilterChangeId", reminderSetGet.getFilterChangeId());
objReminder.put("CustomerId", reminderSetGet.getCustomerId());
jsonArrayReminder.put(objReminder);
} catch (JSONException e) {
}
}
objJobHis.put("Reminders", jsonArrayJob.toString());
jsonArrayJob.put(objJobHis);
}
objCust.putOpt("FilterChanges", jsonArrayJob.toString());
jsonArrayCust.put(objCust);
}
JSONObject finalobject = new JSONObject();
finalobject.put("Final", jsonArrayCust.toString());
} catch (SQLException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
我的要求是按层次顺序表示可能有多个客户表,并且在每个客户表中都会有多个JOB,并且在每个作业中,都会有多个提醒
答案 0 :(得分:0)
尝试这个..这个是基于你的json制作json字符串。
try {
customerDetailsTable = new CustomerDetailsTable(con);
customerDetailsTable.open();
arrayListCustomer = customerDetailsTable.getAllContacts();
customerDetailsTable.close();
JSONObject objCust = null;
JSONArray jsonArrayCust = new JSONArray();
for (int i = 0; i < arrayListCustomer.size(); i++) {
customerDetailsSetGet = arrayListCustomer.get(i);
objCust = new JSONObject();
try {
objCust.put("CustomerId", customerDetailsSetGet.getCustomerID());
objCust.put("PoolGuyId", customerDetailsSetGet.getPoolGuyId());
} catch (JSONException e) {
}
ArrayList<JobHistorySetGet> arrayListjobHistory = new ArrayList<>();
jobHistoryTable = new JobHistoryTable(con);
jobHistoryTable.open();
arrayListjobHistory = jobHistoryTable.getJobsAsPerCustId(customerDetailsSetGet.getCustomerID());
jobHistoryTable.close();
JSONObject objJobHis = null;
JSONArray jsonArrayJob = new JSONArray();
for (int j = 0; j < arrayListjobHistory.size(); j++) {
jobHistorySetGet = arrayListjobHistory.get(j);
objJobHis = new JSONObject();
try {
objJobHis.put("FilterChangeId", jobHistorySetGet.getFilterChangeId());
objJobHis.put("CustomerId", jobHistorySetGet.getCustomerId());
} catch (JSONException e) {
}
ArrayList<ReminderSetGet> arrayListReminder = new ArrayList<>();
reminderTable = new ReminderTable(con);
reminderTable.open();
arrayListReminder = reminderTable.getReminderAsPerJobID(jobHistorySetGet.getId());
reminderTable.close();
JSONArray jsonArrayReminder = new JSONArray();
JSONObject objReminder = null;
for (int k = 0; k < arrayListReminder.size(); k++) {
reminderSetGet = arrayListReminder.get(k);
objReminder = new JSONObject();
try {
Log.d("Anuj", "9");
objReminder.put("ReminderId", reminderSetGet.getReminderId());
objReminder.put("FilterChangeId", reminderSetGet.getFilterChangeId());
objReminder.put("CustomerId", reminderSetGet.getCustomerId());
jsonArrayReminder.put(objReminder);
} catch (JSONException e) {
}
}
objJobHis.put("Reminders",jsonArrayReminder);
jsonArrayJob.put(objJobHis);
}
objCust.putOpt("FilterChanges", jsonArrayJob);
jsonArrayCust.put(objCust);
}
JSONObject finalobject = new JSONObject();
finalobject.put("Final", jsonArrayCust);
} catch (SQLException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}