简体   繁体   English

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

[英]how to parse json array in a json object response?

Iwant to access a jsonArray data in a jsonObjet response code that comes from server.我想访问来自服务器的 jsonObjet 响应代码中的 jsonArray 数据。 here is my response json这是我的回复 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"
}

I want to parse "event_locations_array" and what what i done :我想解析“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();
            }

        }

here i can't take that jsonArray.在这里我不能接受那个 jsonArray。 what i did wrong here let me know .我在这里做错了什么让我知道。 I'm a little confused thanks in advance我有点困惑提前谢谢

Fast solution快速解决方案

If you can not change the JSON generation, simply use this code:如果您无法更改 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();
  }
}

Detailed solution详细解决方案

There are two errors:有两个错误:

  1. The event_locations_array : "[[{"latitude":37.58728984572849].." contains a string. event_locations_array : "[[{"latitude":37.58728984572849].."包含一个字符串。 For the value that it's contains I think it must be generated as array (without the initial and final ")对于它包含的值,我认为它必须作为数组生成(没有初始和最终“)

  2. After fix the first problem, you are trying to extract as object the properties latitude and longitude , but they are attributes.解决第一个问题后,您尝试将属性latitudelongitude提取为对象,但它们是属性。 So change in your code:所以改变你的代码:

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

With

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

To get Values of event_locations_array use following code:要获取event_locations_array值, event_locations_array使用以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM