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:

all_dataisJSONObjectinstead ofJSONArray. useJSONObject jsonObject = new JSONObject(all_data);then usejsonObjectto get all JSONArrays from it