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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值