Android使用自带JSONObject解析JSON数据

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;


   public void allInfoFromJson(String jsonStr){
        /*{   "listMsg":"数据获取成功",
            "list":[
                    {"busId":1,"busLine":"惺惺惜惺惺","busName":"501","cityId":2,"districtId":1,
"firstTime":"8:00","lastTime":"19:00","price":4,"version":0},
                    {"busId":2,"busLine":"502","busName":"502","cityId":2,"districtId":2,
"firstTime":"2","lastTime":"2","price":2,"version":0},
                    {"busId":3,"busLine":"aaa","busName":"601","cityId":2,"districtId":1,
"firstTime":"aa","lastTime":"aa","price":1,"version":0}
                    ],
            "nVersion":3} */
        try {
            //JSONObject jsonObject=new JSONObject(jsonStr).getJSONObject("list");

            JSONArray jsonArray=new JSONObject(jsonStr).getJSONArray("list");
          
            for(int i=0;i<jsonArray.length();i++){

                JSONObject jsonObject=(JSONObject)jsonArray.get(i);

                String busLine=jsonObject.getString("busLine");
                String busName=jsonObject.getString("busName");
                Integer cityId=jsonObject.getInt("cityId");
                Integer districtId=jsonObject.getInt("districtId");
                String firstTime=jsonObject.getString("firstTime");
                String lastTime=jsonObject.getString("lastTime");
                Double price=jsonObject.getDouble("price");

                Bus bus=new Bus(busName,busLine,firstTime,lastTime,price,cityId,districtId);
                BusService busService=new BusService(context);
                busService.save(bus);
            }
            
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值