gpt4 book ai didi

android - 如何在 json 对象响应中解析 json 数组?

转载 作者:行者123 更新时间:2023-11-29 00:55:39 31 4
gpt4 key购买 nike

我想访问来自服务器的 jsonObjet 响应代码中的 jsonArray 数据。这是我的回复 json

{
event_id: "32",
event_title: "امیر",
event_description: "تست امیر",
event_image: "2_1550507094.jpg",
event_hikers_amount: "9",
event_private: "0",
event_date_start: "17/2/2019",
event_date_end: "21/2/2019",
event_time_start: "21:54",
event_time_end: "12:54",
event_locations_array:
"[
{"latitude":37.58728984572849,"longitude":45.10016608983278},
{"latitude":37.57651702299841,"longitude":45.0880378112197},
{"latitude":37.5753956777439,"longitude":45.1045374199748},
{"latitude":37.564077382844964,"longitude":45.094508975744255},
{"latitude":37.55829758877768,"longitude":45.08105669170619},
{"latitude":37.53919984571198,"longitude":45.09874418377876}
]",
event_latitude_location: "37.587289845728",
event_longitude_location: "45.100166089833",
event_status: "1",
event_users_id: "2"
}

我想解析“event_locations_array”以及我所做的:

@Override
protected void onPostExecute(String result) {

try {
JSONObject jsonObject = new JSONObject(result);

description = jsonObject.getString("event_description");
people_joined = jsonObject.getString("event_hikers_amount");
date_start = jsonObject.getString("event_date_start");
date_end = jsonObject.getString("event_date_end");
time_start = jsonObject.getString("event_time_start");
time_end = jsonObject.getString("event_time_end");
privacy = jsonObject.getString("event_private");

JSONArray jsonArray = jsonObject.getJSONArray("event_locations_array");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject points = jsonArray.getJSONObject(i);
JSONObject lat = points.getJSONObject("latitude");
JSONObject lang = points.getJSONObject("longitude");
}

setTextView();

} catch (JSONException e) {
e.printStackTrace();
}

}

这里我不能接受那个 jsonArray。我在这里做错了什么让我知道。我有点困惑提前致谢

最佳答案

快速解决

如果您无法更改 JSON 生成,只需使用此代码:

@Override
protected void onPostExecute(String result) {

try {
JSONObject jsonObject = new JSONObject(result);

description = jsonObject.getString("event_description");
people_joined = jsonObject.getString("event_hikers_amount");
date_start = jsonObject.getString("event_date_start");
date_end = jsonObject.getString("event_date_end");
time_start = jsonObject.getString("event_time_start");
time_end = jsonObject.getString("event_time_end");
privacy = jsonObject.getString("event_private");

// 1 - fix string to array conversion
JSONArray jsonArray = new JSONArray(jsonObject.getString("event_locations_array"));
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject points = jsonArray.getJSONObject(i);
// 2 - the fields as double
double lat = points.getDouble("latitude");
double lang = points.getDouble("longitude");
}

setTextView();

} catch (JSONException e) {
e.printStackTrace();
}
}

详细解决方案

有两个错误:

  1. event_locations_array:[[{"latitude":37.58728984572849].." 包含一个字符串。对于它包含的值,我认为它必须生成为数组(没有初始和最终的 ")

  2. 解决第一个问题后,您尝试将属性 latitudelongitude 提取为对象,但它们是属性。所以改变你的代码:

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject points = jsonArray.getJSONObject(i);
JSONObject lat = points.getJSONObject("latitude");
JSONObject lang = points.getJSONObject("longitude");
}

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject points = jsonArray.getJSONObject(i);
double lat = points.getDouble("latitude");
double lang = points.getDouble("longitude");
}

关于android - 如何在 json 对象响应中解析 json 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54956414/

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