• 7
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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. here is my response 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 :

@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. what i did wrong here let me know . I'm a little confused thanks in advance

      • 1
    • Please post the error message or log you got along with it. PS : It is better to use a combination of gson() library and POJO classes for the parsing. Manual parsing will consume more time and effort.
    • Please share your error,why you dont use GSON ? and i think you have just a json object why you use jsonArray?

Fast solution

If you can not change the JSON generation, simply use this code:

@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. 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. 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");
}
  • 1
Reply Report

To get Values of event_locations_array use following code:

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");
    }
  • 0
Reply Report