java - 如何将嵌套的 JSON 数组存储到模型中
问题描述
你好,所以我有一个模型叫Job
. 该模型收集有关特定工作的所有信息。但是数据库关系的布局方式意味着当我返回作业详细信息时,我会得到一个cargo
嵌套在作业对象中的数组。我想知道如何存储name
嵌套在我的 JSON 对象中的货物数组内部,我想将它存储到我的模型中。
请您花点时间考虑一下我的情况,我已经在 StackOverflow 和其他网站上搜索了解决方案,但他们提供的循环不起作用。希望我可以通过发布自己找到答案
带有嵌套数组的 JSON 对象:
"jobs": [
{
"id": 103080,
"user_id": 496,
"tracker_jobs_id": 91068,
"game_id": 1,
"pickup_city_id": 72,
"destination_city_id": 128,
"cargo_id": 366,
"pickup_company_id": 16,
"destination_company_id": 18,
"date": "2018-11-03",
"distance_driven": 244,
"load_damage": 7,
"estimated_income": 10956,
"total_income": 10956,
"cargo_weight": 24,
"division_load": 0,
"promotional_delivery_id": null,
"another_driver": 0,
"division_id": null,
"convoy_code": null,
"comments": null,
"created_at": "2018-11-04 00:24:42",
"updated_at": "2018-11-04 00:24:42",
"delete": "false",
"status": null,
"cargo": {
"id": 366,
"name": "Square Tubing",
"price_coef": 1,
"fragility": 0.2,
"wotr_only": 0,
"overweight_dlc": 0
},
.... (THEN IT LOOPS WITH THE NEXT JOB)
我的工作模式:
public Job (int id, int user_id, int tracker_jobs_id, int game_id, int pickup_city_id, int destination_city_id,
int cargo_id, int pickup_company_id, int destination_company_id, Date date, int distance_driven, int load_damage,
int estimated_income, int total_income, int cargo_weight, int division_load, int promotional_devlivery_id, int another_driver,
int division_id, String convoy_code, String comments, String delete, String status, JSONArray cargo) {
this.id = id;
this.user_id = user_id;
this.tracker_jobs_id = tracker_jobs_id;
this.game_id = game_id;
this.pickup_city_id = pickup_city_id;
this.destination_city_id = destination_city_id;
this.cargo_id = cargo_id;
this.pickup_company_id = pickup_company_id;
this.destination_company_id = destination_company_id;
this.date = date;
this.distance_driven = distance_driven;
this.load_damage = load_damage;
this.estimated_income = estimated_income;
this.total_income = total_income;
this.cargo_weight = cargo_weight;
this.division_load = division_load;
this.promotional_devlivery_id = promotional_devlivery_id;
this.another_driver = another_driver;
this.division_id = division_id;
this.convoy_code = convoy_code;
this.comments = comments;
this.delete = delete;
this.status = status;
this.cargo = cargo;
}
如您所见,我已经尝试将其存储为 JSONArray 但最终只是空白[]
我如何从我的请求中存储它:
JSONObject jObj = new JSONObject(response);
JSONArray listJobs = jObj.getJSONArray("jobs");
Gson gson = new Gson();
sUserJobs = new ArrayList<>();
for (int i = 0; i < listJobs.length(); i++) {
try {
Job job = gson.fromJson(listJobs.getJSONObject(i).toString(), Job.class);
sUserJobs.add(job);
} catch (JSONException e) {
e.printStackTrace();
}
}
解决方案
您还需要有一个 Cargo 类。然后你需要像下面这样提取货物,同时让 Job 类将货物对象设置为相关对象。
Cargo cargo = gson.fromJson(listJobs.getJSONObject(i).getString("cargo").toString(), cargo.class);
推荐阅读
- python - 查找重复值的索引并将这些索引位置的值添加到不同的列表中
- database - 数据库 - 创建触发器
- sql - 查询在子表单内给出不同结果与仅运行查询时遇到问题
- java - 如何将 Mock 设置为具有默认行为并可以在某些测试中覆盖它
- mongodb - 根据条件从 Mongo DB 聚合中未命名的嵌套对象数组中获取值的键
- python - 如何实现连接 4 的转置表?
- java - 我们可以在已经是一对多的表上设置额外的多对多吗?
- tensorflow - 具有可变长度训练数据的 Tensorflow 2 Keras 模型性能不佳
- apache-kafka - Apache Nifi:有没有办法使用消息键作为多个属性的组合向 kafka 发布消息?
- javascript - FullCalendar 5.0.0-beta.4 ThirdPartyDraggable - 如何延迟点击?