I'm trying to parse a JSON result
fetched from a URL in my app
I have tried a few examples on the Internet, but I couldn't make them working.
The data looks like this:
[{"objectno":"CarlosG","objectname":"MJ1196H00167","objecttype":"car_station_wagon","postext":"11 km norte de Monterrey, en San Nicolás De Los Garza NL, Carretera Monterrey-Nuevo Laredo MEX-85 ","ignition":1,"drivername":"Carlos Gutierrez","longitude_mdeg":-100292728,"latitude_mdeg":25765772},{"objectno":"Demo","objectname":"MJ1196H00169","objecttype":"car","postext":"Adsum Culiacán Rosales, Calle RÃo San Lorenzo ","ignition":1,"drivername":"AutoDemo","longitude_mdeg":-107392146,"latitude_mdeg":24794140},{"objectno":"GustavoC","objectname":"MJ6146H01742","objecttype":"car_station_wagon","postext":"En Guadalajara JA, Calle George Frideric Handel / Calle A Las Montañas ","ignition":0,"drivername":"Gustavo Cortez","longitude_mdeg":-103421146,"latitude_mdeg":20675859}]
JSON FORMATTED
[
{
"objectno":"CarlosG",
"objectname":"MJ1196H00167",
"objecttype":"car_station_wagon",
"postext":"11 km norte de Monterrey, en San Nicolás De Los Garza NL, Carretera Monterrey-Nuevo Laredo MEX-85 ",
"ignition":1,
"drivername":"Carlos Gutierrez",
"longitude_mdeg":-100292728,
"latitude_mdeg":25765772
},
{
"objectno":"Demo",
"objectname":"MJ1196H00169",
"objecttype":"car",
"postext":"Adsum Culiacán Rosales, Calle RÃo San Lorenzo ",
"ignition":1,
"drivername":"AutoDemo",
"longitude_mdeg":-107392146,
"latitude_mdeg":24794140
},
{
"objectno":"GustavoC",
"objectname":"MJ6146H01742",
"objecttype":"car_station_wagon",
"postext":"En Guadalajara JA, Calle George Frideric Handel / Calle A Las Montañas ",
"ignition":0,
"drivername":"Gustavo Cortez",
"longitude_mdeg":-103421146,
"latitude_mdeg":20675859
}
]
I need it in a listview or a spinner.
What's the simplest way to fetch the URL and parse the JSON data show it in the listview
or spinner
?
Thanks
You have to create a modal class and List View Adapter.
Model.java
public class Model {
String field1, field2,... fieldn;
public Model(JSONObject object) {
field1 = object.optString("key1");
field1 = object.optString("key2");
.
.
fieldn = object.optString("keyn");
}
//getter methods of each fields (You've an option to auto generate them in Android Studio
}
Now from the Activity class create an ArrayList like this
ArrayList<Model> models = new ArrayList<>;
for (int i = 0; i < jsonArray.length(); i++) {
models.add(new Model(jsonArray.getJSONObject()));
}
//Populate List View
ListView lv = findViewById(R.id.lv);
CustomAdapter adapter = new CustomAdapter(models);
lv.setAdapter(adapter);
//Create a new class for the Custom adapter extending the BaseAdapter
class CustomAdapter extends BaseAdapter {
ArrayList<Model> models;
public CustomAdapter(ArrayList<Model> models) {
this.models = models;
}
View getView(...) {
//Create your item view here
// Use a ViewHolder to hold the Views
//Access the data from the model
Model model = (Model) getItem(position);
holder.textView.setText(model.getField1());
........
........
return convertView;
}
Object getItem(int position) {
return models.get(position);
}
}
I've given a model. Modify the classes to incorporate your changes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.