zoukankan      html  css  js  c++  java
  • 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();
            }
        }
  • 相关阅读:
    jquery选择器
    js实现添加className
    日期函数(date)
    IE6和IE7中<a>标签宽高设置无效的问题
    Uva 548 二叉树的递归遍历lrj 白书p155
    Uva 122 树的层次遍历 Trees on the level lrj白书 p149
    Uva 679 Dropping Ballls 二叉树的编号
    Uva 12657 Boxes in a Line 双向链表
    Uva 11988 Broken Keyboard STL+链表
    埃及分数问题+迭代加深搜索
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4644567.html
Copyright © 2011-2022 走看看