Reputation:
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:
Upvotes: 4
Views: 274
Reputation: 1108
You can create a class with the JSON structure and use GSON to get the data like User user = gson.fromJson(json, User.class)
Upvotes: 0
Reputation: 7209
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
Upvotes: 0
Reputation: 3444
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
Upvotes: -1
Reputation: 75788
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//..........
Upvotes: 3
Reputation: 560
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");
}
Upvotes: 0