4

I'm fairly new to Android dev, and am trying to write a programme to parse some JSON from a website and output it in a ListView. However, when I run my programme, I get the error: (There are more then 7 row same as this error)

03-31 05:25:14.296 3196-3196/nazilli.tenispark E/FAILED: Json parsing error: Value {"0":"2","id":"2","1":"0","cid":"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: Json parsing error: Value {"0":"3","id":"3","1":"0","cid":"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

The JSON I'm trying to parse is:

{"appointments":[{"0":"2","id":"2","1":"0","cid":"0","2":"1","uid":"1","3":"2017-04-01","date":"2017-04-01","4":"20","hour":"20"},{"0":"3","id":"3","1":"0","cid":"0","2":"1","uid":"1","3":"2017-04-08","date":"2017-04-08","4":"20","hour":"20"},{"0":"4","id":"4","1":"0","cid":"0","2":"1","uid":"1","3":"2017-04-15","date":"2017-04-15","4":"20","hour":"20"},{"0":"5","id":"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","id":"7","1":"0","cid":"0","2":"1","uid":"1","3":"2017-03-26","date":"2017-03-26","4":"17","hour":"17"},{"0":"8","id":"8","1":"1","cid":"1","2":"1","uid":"1","3":"2017-03-26","date":"2017-03-26","4":"16","hour":"16"},{"0":"9","id":"9","1":"2","cid":"2","2":"1","uid":"1","3":"2017-03-26","date":"2017-03-26","4":"15","hour":"15"},{"0":"10","id":"10","1":"3","cid":"3","2":"1","uid":"1","3":"2017-03-26","date":"2017-03-26","4":"13","hour":"13"}]}

this my listview custom row java

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;
    }
}

This is 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();
        }
    }
}

If I don't parse:

enter image description here

3
  • Becuase all_data is JSONObject instead of JSONArray. use JSONObject jsonObject = new JSONObject(all_data); then use jsonObject to get all JSONArrays from it Commented Mar 31, 2017 at 5:47
  • Thanks for quick reply, how can i fix this. Commented Mar 31, 2017 at 5:48
  • 1
    check my answer. click accept if it helps Commented Mar 31, 2017 at 6:02

5 Answers 5

3

Your appointments is JSONArray

JSON

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

 JSONObject reader = new JSONObject(all_data);
 JSONArray jsonArray = reader.getJSONArray("appointments");
  ......//Do your work//..........
Sign up to request clarification or add additional context in comments.

2 Comments

It gave an error E/FAILED: Json parsing error: No value for appointments
No I'm trying answers one by one.
0
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");
}

1 Comment

Still i got an error E/FAILED: Json parsing error: No value for appointments
0
JSONObject json = new JSONObject(all_data);
 JSONArray jsonArray = json.getJSONArray("appointments");

You should get array from the all_data, and use that one instead of json

Comments

0

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

Comments

-1

Your all data has following data.like below.

 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"}

so each time your list item get the data from that appointment array. so alldata variable will be changed based on the position from the getItem(position) method.

So, alldata is jsonObject.

so you should parse like below,

 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();
   }

like you have to call. hope it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.