gpt4 book ai didi

java - 如何将嵌套 JSON 数组存储到模型中

转载 作者:太空宇宙 更新时间:2023-11-04 10:04:08 25 4
gpt4 key购买 nike

您好,我有一个名为 Job 的模型。该模型收集了有关特定工作的所有信息。但是数据库关系的布局方式意味着当我返回作业详细信息时,我会得到一个嵌套在作业对象内的 cargo 数组。我想知道如何将 name 存储在嵌套在 JSON 对象内的 cargo 数组中,我想将其存储到我的模型中。

请您花点时间考虑一下我的情况,我已经在 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 类。然后,您需要像下面一样提取 cargo ,同时获取作业类并将 cargo 对象设置为相关对象。

Cargo cargo = gson.fromJson(listJobs.getJSONObject(i).getString("cargo").toString(), cargo.class);

关于java - 如何将嵌套 JSON 数组存储到模型中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53137214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com