josn解析常见的几种方法

json解析方式一:

{
    "message": "查询成功",
    "status": "10000",
    "list": [
        {
            "withdraw_type": "3",
            "withdraw_bankcard": "6214836550378968",
            "withdraw_date": "2015-5-24 07:24:04",
            "withdraw_money": "7000"
        },
        {
            "withdraw_type": "2",
            "withdraw_bankcard": "6214836550378968",
            "withdraw_date": "2015-5-24 07:22:57",
            "withdraw_money": "4000"
        },
        {
            "withdraw_type": "1",
            "withdraw_bankcard": "6214836550378968",
            "withdraw_date": null,
            "withdraw_money": "5000"
        },
        {
            "withdraw_type": "4",
            "withdraw_bankcard": "6214836550378968",
            "withdraw_date": "2015-5-24 07:25:12",
            "withdraw_money": "5000"
        }
    ]
}

 解析方法:

String result = Net.GetTixianMoneyDetail(Constant.getTixianMoneyDetail,
							"sigen",ph.getValue("sigen"));
					result = result.substring(1, result.length() - 1);
					Log.i("结果", result);
					JSONObject jsonObject = new JSONObject(result);
					JSONArray array = jsonObject.getJSONArray("list");
					JSONObject jsonObject2;
					for(int i = 0;i<array.length();i++){
					    map =  new HashMap<String, String>();
						jsonObject2 = array.getJSONObject(i);
						map.put("withdraw_money", jsonObject2.getString("withdraw_money"));
						map.put("bankno", jsonObject2.getString("withdraw_bankcard"));
						map.put("withdraw_type",jsonObject2.getString("withdraw_type"));
						map.put("time", jsonObject2.getString("withdraw_date"));
						list.add(map);
					}

json解析方式二:

{
    "message": "查询成功",
    "list4": [
        {
            "total": "0.0",
            "day": "2015-05-14",
            "trade_type2": "0.0",
            "trade_type1": "0.0",
            "trade_type3": "0.0"
        }
    ],
    "list5": [
        {
            "total": "0.0",
            "day": "2015-05-15",
            "trade_type2": "0.0",
            "trade_type1": "0.0",
            "trade_type3": "0.0"
        }
    ],
    "list2": [
        {
            "total": "1000.0",
            "day": "2015-05-12",
            "trade_type2": "1000.0",
            "trade_type1": "0.0",
            "trade_type3": "0.0"
        }
    ],
    "list3": [
        {
            "total": "1000.0",
            "day": "2015-05-13",
            "trade_type2": "0.0",
            "trade_type1": "0.0",
            "trade_type3": "1000.0"
        }
    ],
    "status": "10000",
    "list1": [
        {
            "total": "940.0",
            "day": "2015-05-11",
            "trade_type2": "0.0",
            "trade_type1": "940.0",
            "trade_type3": "0.0"
        }
    ],
    "list6": [
        {
            "total": "0.0",
            "day": "2015-05-16",
            "trade_type2": "0.0",
            "trade_type1": "0.0",
            "trade_type3": "0.0"
        }
    ],
    "list7": [
        {
            "total": "0.0",
            "day": "2015-05-17",
            "trade_type2": "0.0",
            "trade_type1": "0.0",
            "trade_type3": "0.0"
        }
    ]
}

  解析方法:

	String result = Net.SetDataZhuXing(Constant.getWeekData,
							"sigen", sigen, "date", date);
					result = result.substring(1, result.length() - 1);
					Log.i("数据结果", result);
					
					JSONObject jsonObject = new JSONObject(result);
					JSONArray list1 = jsonObject.getJSONArray("list1");
					JSONArray list2 = jsonObject.getJSONArray("list2");
					JSONArray list3 = jsonObject.getJSONArray("list3");
					JSONArray list4 = jsonObject.getJSONArray("list4");
					JSONArray list5 = jsonObject.getJSONArray("list5");
					JSONArray list6 = jsonObject.getJSONArray("list6");
					JSONArray list7 = jsonObject.getJSONArray("list7");

					JSONArray[] jsonarray = new JSONArray[] { list1, list2,
							list3, list4, list5, list6, list7 };
					JSONObject jsonObject1 = new JSONObject();
					for (int i = 0; i < jsonarray.length; i++) {
						map = new HashMap<String, String>();
						jsonObject1 = (JSONObject) jsonarray[i].opt(0);
						map.put("total", jsonObject1.getString("total"));// 视频id
						map.put("day", jsonObject1.getString("day"));// 图片名字
						map.put("trade_type2",
								jsonObject1.getString("trade_type2"));// 图片地址
						map.put("trade_type3",
								jsonObject1.getString("trade_type3"));// 积分
						map.put("trade_type1",
								jsonObject1.getString("trade_type1"));// 图片
						list_two.add(map);
					}

json解析方式三:

[
    {
        "message": "查询成功",
        "isFriday": "1",
        "balance_position": "0.0",
        "status": "10000",
        "sum_bonus": "0",
        "balance_recommend": "2115.00",
        "IS_Last": "0",
        "balance_manager": "211.50"
    }
]

解析的方法:

String result = Net.GetTixianMoney(Constant.getTixianMoney,
							"sigen", sigenNum);
					result = result.substring(1, result.length() - 1);
					Log.i("登录结果", result);
					JSONObject jsonObject = new JSONObject(result);
					chuangye_tianxian = jsonObject
							.getString("balance_recommend");// 可提现创业奖金(元)
					shichang_tixiang = jsonObject.getString("balance_position");// 可提现市场区域奖励(元)
					guanli_tixiang = jsonObject.getString("balance_manager");// 可提现管理补贴(元)
					fenghong_tixiang = jsonObject.getString("sum_bonus");// 可提现创业分红(元)
					//today_server = jsonObject.getString("date");// 可提现创业分红(元)
					isLast = jsonObject.getString("IS_Last");// 1表示不是月末
					isFriday = jsonObject.getString("isFriday");// 可提现创业分红(元)

 

总结:一层一层剥开。

        比如方式三: 没有json数组,就直接包装成json 对象。JSONObject jsonObject = new JSONObject(result);然后getString取出来

        比如方式一:里面包含一个json数组,

                       那么先把最外层JSONObject jsonObject = new JSONObject(result);包装成json格式。

                        再从jsonObject中得到json数组, JSONArray array = jsonObject.getJSONArray("list");
                       然后遍历。
       比如方式三:里面包含多个json数组,那么就把jsonArray放入到JsonArray[]中去。然后每个jsonArray遍历

原文地址:https://www.cnblogs.com/childhooding/p/4505711.html