JSON parsing error for Android cannot be converted to JSONArray

I am new to Android dev and I am trying to write a program to parse JSON from a website and output it to a ListView. However, when I run my program, I get an error: (There are more than 7 lines, the same as this error)

03-31 05: 25: 14.296 3196-3196 / nazilli.tenispark E / FAILED: Json parsing error: value {"0": "2", "identifier": "2", "1": "0", " chid ":" 0 "," 2 ":" 1 "," UID ":" 1 "," 3 ":" 2017-04-01 "," date ":" 2017-04-01 "," 4 " : "20", "hour": "20"} of type org.json.JSONObject cannot be converted to JSONArray

03-31 05: 25: 14.297 3196-3196 / nazilli.tenispark E / FAILED: parsing error Json: Cost {"0": "3", "identifier": "3", "1": "0", " chid ":" 0 "," 2 ":" 1 "," UID ":" 1 "," 3 ":" 2017-04-08 "," date ":" 2017-04-08 "," 4 " : "20", "hour": "20"} of type org.json.JSONObject cannot be converted to JSONArray

I am trying to parse JSON:

{"destination": [{"0": "2", "identifier": "2", "1": "0", "cid": "0", "2": "1", "UID" : "1", "3": "2017-04-01", "date": "2017-04-01", "4": "20", "hour": "20"}, {"0" : "3", "identifier": "3", "1": "0", "chid": "0", "2": "1", "UID": "1", "3": " 2017-04-08 "," date ":" 2017-04-08 "," 4 ":" 20 "," hour ":" 20 "}, {" 0 ":" 4 "," identifier ":" 4 "," 1 ":"0 "," chid ":" 0 "," 2 ":" 1 "," UID ":" 1 "," 3 ":" 2017-04-15 "," date ":" 2017-04-15 " , "4": "20", "hour": "20"}, {"0": "5", "identifier": "5", "1": "0", "cid": "0" , "2": "1", "UID": "1", "3": "2017-04-22", "date": "2017-04-22" "4": "20", "hour ":" 20 "}, {" 0 ":" 6 "," ID ":" 6 "," 1 ":" 0 "," cid ":" 0 "," 2 ":" 1 "," UID ":" 1 "," 3 ":" 2017-03-24 ","date": "2017-03-24", "4": "17", "hour": "17"}, {"0": "7", "identifier": "7", "1": "0", "chid": "0", "2": "1", "UID": "1", "3": "2017-03-26", "date": "2017-03-26 "," 4 ":" 17 "," hour ":" 17 "}, {" 0 ":" 8 "," identifier ":" 8 "," 1 ":" 1 "," chid ":" 1 "," 2 ":" 1 "," UID ":" 1 "," 3 ":" 2017-03-26 "," date ":" 2017-03-26 "," 4 ":" 16 ", "hour": "16"}, {"0": "9 "," identifier ":" 9 "," 1 ":" 2 "," chid ":" 2 "," 2 ":" 1 "," UID ":" 1 "," 3 ":" 2017- 03-26 "," date ":" 2017-03-26 "," 4 ":" 15 "," hour ":" 15 "}, {" 0 ":" 10 "," identifier ":" 10 " , "1": "3", "chid": "3", "2": "1", "UID": "1", "3": "2017-03-26", "yes ":" 2017-03-26 "," 4 ":" 13 "," hour ":" 13 "}]}UID ":" 1 "," 3 ":" 2017-03-26 "," date ":" 2017-03-26 "," 4 ":" 15 "," hour ":" 15 "}, {" 0 ":" 10 "," identifier ":" 10 "," 1 ":" 3 "," chid ":" 3 "," 2 ":" 1 "," UID ":" 1 "," 3 " : "2017-03-26", "yes, that is": "2017-03-26", "4": "13", "hour": "13"}]}UID ":" 1 "," 3 ":" 2017-03-26 "," date ":" 2017-03-26 "," 4 ":" 15 "," hour ":" 15 "}, {" 0 ":" 10 "," identifier ":" 10 "," 1 ":" 3 "," chid ":" 3 "," 2 ":" 1 "," UID ":" 1 "," 3 " : "2017-03-26", "yes, that is": "2017-03-26", "4": "13", "hour": "13"}]}3 "," 2 ":" 1 "," UID ":" 1 "," 3 ":" 2017-03-26 "," yes, that is ":" 2017-03-26 "," 4 ": "13", "hour": "13"}]}3 "," 2 ":" 1 "," UID ":" 1 "," 3 ":" 2017-03-26 "," yes, that is ":" 2017-03-26 "," 4 ": "13", "hour": "13"}]}

java listview

public class adapter_appointment extends ArrayAdapter<String> {
    public adapter_appointment(Context context, String[] data){
        super(context, R.layout.row_layout, data);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View customView = inflater.inflate(R.layout.row_layout, parent, false);
        String all_data = getItem(position);
        TextView title = (TextView) customView.findViewById(R.id.title);
        //title.setText(all_data.toString());
        try {
            JSONArray array = new JSONArray(all_data);
            JSONObject obj = array.getJSONObject(0);
            Log.d("SUCCESS", "JSON Object: " + obj.toString());
            if (obj.has("date") && !obj.isNull("date")) {
                title.setText(obj.getString("date").toString());
                Log.d("SUCCESS", "Date: " + obj.getString("date").toString());
            } else {
                // Do something
            }
        } catch (Exception e) {
            Log.e("FAILED", "Json parsing error: " + e.getMessage());
        }
        return customView;
    }
}

json.java

public class my_appointments extends AppCompatActivity {
    ListView lv;
    InputStream is = null;
    String line = null;
    String result = null;
    String[] data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_appointments);

        lv=(ListView) findViewById(R.id.my_appointments);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
        //Run
        getData();
        ArrayAdapter adapter = new adapter_appointment(this, data);
        lv.setAdapter(adapter);
    }

    private void getData()
    {
        try {
            URL url = new URL("MY URL");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            is=new BufferedInputStream(con.getInputStream());
            //
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            while((line=br.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
            //
            JSONObject jo = new JSONObject(result);
            JSONArray ja = jo.getJSONArray("appointments");
            data = new String[ja.length()];
            for(int i=0;i<ja.length();i++)
            {
                jo=ja.getJSONObject(i);
                data[i]=jo.toString();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

:

enter image description here

+4
5

. .

 alldata={"0":"2","id":"2","1":"0","cid":"0","2":"1","uid":"1","3":"2017-04-01","date":"2017-04-01","4":"20","hour":"20"}

, . alldata getItem (position).

, alldata - jsonObject.

, ,

 JSONObject jsonRowData= new JSONObject(allData);
 try{
   jsonRowData.getString("0");
   jsonRowData.getString("id"):
   jsonRowData.getString("1"):
   jsonRowData.getString("cid"):
   jsonRowData.getString("2"):
   jsonRowData.getString("uid"):
   jsonRowData.getString("3");
   jsonRowData.getString("date"):
   jsonRowData.getString("4");
    }catch(Exception e){
    e.printStackTrace();
   }

. ,

-1

appointments - JSONArray

JSON

{ "": [{ "0" : "2" , "": "2" , "1" : "0" , "": "0" , "2" : "1" , "UID": "1" , "3": "2017-04-01", "": "2017-04-01", "4": "20", "": "20" }]}

 JSONObject reader = new JSONObject(all_data);
 JSONArray jsonArray = reader.getJSONArray("appointments");
  ......//Do your work//..........
+2
JSONObject jsonObject= new JSONObject(all_data);

JSONArray jsonArray = jsonObject.getJSONArray("appointments");
 // To get elements from the array 
for(int i=0;i<jsonArray.length;i++){
  JSONObject json = jsonArray.getJSONObject(i);
  String id = json.getString("id");
  String name=json.getString("cid");
}
0
source
JSONObject json = new JSONObject(all_data);
 JSONArray jsonArray = json.getJSONArray("appointments");

You should get an array from all_data and use it instead of json

0
source

You can create a class with a JSON structure and use GSON to get data of type User user = gson.fromJson(json, User.class)

0
source

Source: https://habr.com/ru/post/1673673/


All Articles