将JSON PARS到ListView

Bra*_*son 2 android

好吧,我已经通过大量关于PARSing我的JSON结果的例子,没有运气.我有以下JSON示例,我现在不想要状态信息或geoLocation.我只想使用station对象并从数组中选择一些东西以ListView的形式显示为列表.我不明白那里的任何文件.

有人能够提供一个关于如何读取JSON并将其放入ListView的简单示例.这将非常有帮助.我真的没有任何代码,因为它对我来说都不起作用.

{
"status": {
    "error": "NO",
    "code": 200,
    "description": "none",
    "message": "Request ok"
},
"geoLocation": {
    "city_id": "147",
    "city_long": "Saint-Laurent",
    "region_short": "QC",
    "region_long": "Quebec",
    "country_long": "Canada",
    "country_id": "43",
    "region_id": "35"
},
"stations": [
    {
        "country": "Canada",
        "reg_price": "N\/A",
        "mid_price": "N\/A",
        "pre_price": "N\/A",
        "diesel_price": "N\/A",
        "address": "3885, Boulevard de la C\u00f4te-Vertu",
        "diesel": "0",
        "id": "33862",
        "lat": "45.492367",
        "lng": "-73.710915",
        "station": "Shell",
        "logo": "http:\/\/www.mygasfeed.com\/img\/station-logo\/logo-shell.png",
        "region": "Quebec",
        "city": "Saint-Laurent",
        "reg_date": "N\/A",
        "mid_date": "N\/A",
        "pre_date": "N\/A",
        "diesel_date": "N\/A",
        "distance": "1.9km"
    }
]
Run Code Online (Sandbox Code Playgroud)

Cas*_*mer 6

首先解析你的JSON对象,如下所示:

String str_json = "your json string";
try {
    JSONObject obj = new JSONObject(str_json);
    JSONArray stations = obj.getJSONArray("stations");
    //etc etc...

} catch (JSONException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

然后将此JSONArray解析为您创建的自定义Station对象的ArrayList,例如:

public class Station {

    public String country;
    public int reg_price;
    // etc etc...
}
Run Code Online (Sandbox Code Playgroud)

将JSONArray中的项放入ArrayList:

ArrayList<Station> stationsArrList = new ArrayList<Station>();

int len = stations.size();    
for ( int i = 0; i < len; i++ ){
    JSONObject stationObj = stations.getJSONObject(i);
    Station station = new Station();

    for ( int j = 0; j < stationObj.len(); j++ ){
        //add items from stationObj to station
    }
    stationsArrList.add(station);
}
Run Code Online (Sandbox Code Playgroud)

然后创建一个适配器(假设您希望显示两个以上的信息):

public class StationListAdapter extends BaseAdapter {
    private static ArrayList<Station> stationArrayList;

    private LayoutInflater inflator;

    public StationListAdapter(Context context, ArrayList<Station> results) {
        stationArrayList = results;
        inflator = LayoutInflater.from(context);
    }

    public int getCount() {
        if (stationArrayList == null)
            return 0;
        else
            return stationArrayList.size();
    }

    public Object getItem(int position) {
        try {
            return stationArrayList.get(position);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflator.inflate(R.layout.list_item_station, null);
            holder = new ViewHolder();
            holder.country = (TextView) convertView.findViewById(R.id.station_listview_item_one);
            holder.reg_price = (TextView) convertView.findViewById(R.id.station_listview_item_two);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.country.setText(stationArrayList.get(position).getCountry());
        holder.reg_price.setText( stationArrayList.get(position).getRegPrice());

        return convertView;
    }

    static class ViewHolder {
        TextView country;
        TextView reg_price;
        //etc
    }
}
Run Code Online (Sandbox Code Playgroud)

在适配器中,您将使用将为每个列表行定义的listview xml布局.

最后,您获得对列表的引用并在主活动代码上添加数据:

stationList = (ListView) findViewById(R.id.station_list_view);
stationListAdapter = new StationListAdapter(this, stationsArrList);
stationList.setAdapter(stationListAdapter);
Run Code Online (Sandbox Code Playgroud)