Android初识-JSON字符串解析详解

来源:互联网 发布:少女前线枪娘数据大全 编辑:程序博客网 时间:2024/06/08 17:51

Json数据结构

{"ResultCode": 1,"Result": {"RecordNum": 4,"ProductNum": 0,"SnNum": 0,"RecordList": [{"Id": "222000002","Name": "沃x金x大酒店","Price": 85.0,"ImageUrl": "http://image.elifemart.com/images/SdImage/Resolution/","ShopCode": "SHOP150500184","ShopName": "沃x金x大酒店中x餐厅","IsStored": "0","MaxPrice": 85.0,"MARKET_PRICE": 100.0,"Shop_address": "xxx","SALEQTY": "124","PDT_INTRODUCTION": "","LATITUDE": "34.572916","LONGITUDE": "113.866047","Distance": 12185.0357,"ISUSE_VIRTUAL": 1.0},{"Id": "222800003","Name": "YHITE3代x金券01","Price": 9.90,"ImageUrl": "xxx","ShopCode": "SHOP150600560","ShopName": "优优KV","IsStored": "0","MaxPrice": 9.90,"MARKET_PRICE": 10.0,"Shop_address": "xxx","SALEQTY": "19","PDT_INTRODUCTION": "YHITE3代金券01","LATITUDE": "34.75661","LONGITUDE": "113.649644","Distance": 12160.8489,"ISUSE_VIRTUAL": 0.0}]}}

解析代码

public static List<ProductItem> parseJsonMulti(String strResult) { List<ProductItem> plist = new ArrayList<ProductItem>();        try {        //获取根对象        JSONObject jsonObj = new JSONObject(strResult);        //获取根对象下的内容        Integer ResultCode = (Integer) jsonObj.get("ResultCode");                //获取根对象下一级对象        JSONObject jsonResult = jsonObj.getJSONObject("Result");        //获取根对象下一级对象的内容        Integer RecordNum = (Integer) jsonResult.get("RecordNum");        Integer ProductNum = (Integer) jsonResult.get("ProductNum");        Integer SnNum = (Integer) jsonResult.get("SnNum");        //获取根对象下一级对象中的数组对象        JSONArray jsonResults = jsonResult.getJSONArray("RecordList");        //初始化数据到对象中            for(int i = 0; i < jsonResults.length() ; i++){               ProductItem pi = new ProductItem();                JSONObject jsonOne = (JSONObject)jsonResults.get(i);                   String ProductId = (String) jsonOne.get("Id");                String ProductName = (String) jsonOne.get("Name");                String ProductDes = (String) jsonOne.get("PDT_INTRODUCTION");                Double SalePrice = (Double) jsonOne.get("Price");                Double MarketPrice = (Double) jsonOne.get("MARKET_PRICE");                String SaleQty = (String) jsonOne.get("SALEQTY");                String ImgUrl = (String) jsonOne.get("ImageUrl");                                pi.setImgUrl(ImgUrl);                pi.setMarketPrice(MarketPrice);                pi.setProductDes(ProductDes);                pi.setProductId(ProductId);                pi.setProductName(ProductName);                pi.setSalePrice(SalePrice);                pi.setSaleQty(SaleQty);                plist.add(pi);            }            return plist;        } catch (JSONException e) {               System.out.println("Jsons parse error !");               e.printStackTrace();           }           return null;    }   


0 0