JSON解析小总结
2018-02-02 09:36
197 查看
转载:http://blog.csdn.net/u011480946/article/details/53470421
数据格式: {“name”:”zhangsanfeng”,”age”:3,”sex”:”nv”}
JSON格式:
[
{“name”:”zhangsanfeng”,”age”:3,”sex”:”nv”},
{“name”:”zhaobenshan”,”age”:2,”sex”:”renyao”}
]
JSON格式:
{
“error_code”: 0,
“reason”: “查询成功!”,
“result”: {
“future”: [
{
“date”: “20140804”,
“temperature”: “28℃~36℃”,
“weather”: “晴转多云”,
“weather_id”: {
“fa”: “00”,
“fb”: “01”
},
“week”: “星期一”,
“wind”: “南风3-4级”
},
{
“date”: “20140805”,
“temperature”: “28℃~36℃”,
“weather”: “晴转多云”,
“weather_id”: {
“fa”: “00”,
“fb”: “01”
},
“week”: “星期二”,
“wind”: “东南风3-4级”
},
{
“date”: “20140806”,
“temperature”: “27℃~35℃”,
“weather”: “晴转多云”,
“weather_id”: {
“fa”: “00”,
“fb”: “01”
},
“week”: “星期三”,
“wind”: “东南风3-4级”
},
{
“date”: “20140807”,
“temperature”: “27℃~34℃”,
“weather”: “多云”,
“weather_id”: {
“fa”: “01”,
“fb”: “01”
},
“week”: “星期四”,
“wind”: “东南风3-4级”
},
“resultcode”: “200”
}
JSON格式:
{
“cookie”: {
“originalMaxAge”: null,
“expires”: null,
“secure”: false,
“httpOnly”: true,
“path”: “/”
},
“user”: {
“integralLevel”: 4,
“integral”: “309.2”,
“integralLimit”: “500”,
“level”: “3”,
“email”: null,
“headSmallPic”: null,
“headMediumPic”: null,
“mobile”: “13402082180”,
“name”: “shlt”,
“nickname”: “shlt”,
“passId”: “7328058863216”,
“uSessionId”: “UDnid0000001517405981291BcxMw2YRFpN33Tc5OdgT78N7qqFeZbFu”,
“uid”: “13289164”,
“platinum”: 0,
“crbt”: 1,
“crbtMonthly”: 1,
“vrbt”: 0,
“miguTotal”: “227”,
“rechargeUrl”: null,
“musician”: 0,
“vipInterest”: {
“opened”: [{
“id”: 3,
“purviewName”: “xe5xbdxa9xe9x93x83xe5x8cx85xe6x9cx88”,
“purviewDepict”: “xe9xabx98xe6xb8x85xe5xbdxa9xe9x93x83xe5x85x8dxe8xb4xb9xe8xaexa2xe8xb4xadrn| DIYxe5xbdxa9xe9x93x83xe5x85x8dxe8xb4xb9xe4xbdxbfxe7x94xa8”,
“hasOpened”: 1
}],
“noOpened”: [{
“id”: 2,
“purviewName”: “xe7x99xbdxe9x87x91xe4xbcx9axe5x91x98”,
“purviewDepic
4000
t”: “xe9xabx98xe6xb8x85xe3x80x81xe6x97xa0xe6x8dx9fxe6xadx8cxe6x9bxb2xe5x85x8dxe8xb4xb9xe4xb8x8bxe8xbdxbdrn| xe8xb6x85xe6xb8x85MVxe5x85x8dxe8xb4xb9xe8xa7x82xe7x9cx8brn|xe4xb8x93xe4xbaxabxe7x9bxb4xe6x92xadxe5x9cxbaxe6xacxa1xe3x80x81xe7x9bxb4xe6x92xadxe7x89xb9xe6x9dx83rn| xe7xa5xa8xe5x8axa1xe5x8dxa1xe3x80x81xe5x92xaaxe5x92x95xe5x88xb8xe5x85x8dxe8xb4xb9xe9xa2x86rn| xe7x9bxb4xe6x92xadxe4xbaxabxe5x85xa8xe7xa8x8bxe6x97xa0xe5xb9xbfxe5x91x8arn| xe7x9bxb4xe6x92xadxe4xbaxabxe5xbcxb9xe5xb9x95xe7x89xb9xe6x95x88”,
“hasOpened”: 0
}]
}
}
}
解析得到json中uSessionId的值:
用工具对JSON解析
1,对象
2,数组
3,综合
数据格式: {“name”:”zhangsanfeng”,”age”:3,”sex”:”nv”}
protected User readJsonObject(String jsonData) { // 内置解析方法JSONObject User bean = null; try { JSONObject jsonObject = new JSONObject(jsonData); String xname = jsonObject.getString("name"); int xage = jsonObject.getInt("age"); String xsex = jsonObject.getString("sex"); bean = new User(xname, xage, xsex); } catch (Exception e) { e.printStackTrace(); } return bean; }
JSON格式:
[
{“name”:”zhangsanfeng”,”age”:3,”sex”:”nv”},
{“name”:”zhaobenshan”,”age”:2,”sex”:”renyao”}
]
protected List<User> readJsonArray(String jsonData) { // 内置解析方法JSONObject List<User> users = new ArrayList<User>(); try { JSONArray jsonArray = new JSONArray(jsonData); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String xname = jsonObject.getString("name"); int xage = jsonObject.getInt("age"); String xsex = jsonObject.getString("sex"); User user = new User(xname, xage, xsex); users.add(user); } } catch (Exception e) { e.printStackTrace(); } return users; }
JSON格式:
{
“error_code”: 0,
“reason”: “查询成功!”,
“result”: {
“future”: [
{
“date”: “20140804”,
“temperature”: “28℃~36℃”,
“weather”: “晴转多云”,
“weather_id”: {
“fa”: “00”,
“fb”: “01”
},
“week”: “星期一”,
“wind”: “南风3-4级”
},
{
“date”: “20140805”,
“temperature”: “28℃~36℃”,
“weather”: “晴转多云”,
“weather_id”: {
“fa”: “00”,
“fb”: “01”
},
“week”: “星期二”,
“wind”: “东南风3-4级”
},
{
“date”: “20140806”,
“temperature”: “27℃~35℃”,
“weather”: “晴转多云”,
“weather_id”: {
“fa”: “00”,
“fb”: “01”
},
“week”: “星期三”,
“wind”: “东南风3-4级”
},
{
“date”: “20140807”,
“temperature”: “27℃~34℃”,
“weather”: “多云”,
“weather_id”: {
“fa”: “01”,
“fb”: “01”
},
“week”: “星期四”,
“wind”: “东南风3-4级”
},
“resultcode”: “200”
}
protected List<Future> readJsonObjArr(String jsonData) { // 内置解析方法JSONObject List<Future> futures = new ArrayList<Future>(); try { JSONObject jsonObject = new JSONObject(jsonData); JSONObject result = jsonObject.getJSONObject("result"); JSONArray jsonArray = result.getJSONArray("future"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject future = jsonArray.getJSONObject(i); String xdate = future.getString("date"); String xtemperature = future.getString("temperature"); String xweather = future.getString("weather"); String xweek = future.getString("week"); String xwind = future.getString("wind"); Future f = new Future(xdate, xtemperature, xweather, xweek, xwind); futures.add(f); } } catch (Exception e) { e.printStackTrace(); } return futures; }
JSON格式:
{
“cookie”: {
“originalMaxAge”: null,
“expires”: null,
“secure”: false,
“httpOnly”: true,
“path”: “/”
},
“user”: {
“integralLevel”: 4,
“integral”: “309.2”,
“integralLimit”: “500”,
“level”: “3”,
“email”: null,
“headSmallPic”: null,
“headMediumPic”: null,
“mobile”: “13402082180”,
“name”: “shlt”,
“nickname”: “shlt”,
“passId”: “7328058863216”,
“uSessionId”: “UDnid0000001517405981291BcxMw2YRFpN33Tc5OdgT78N7qqFeZbFu”,
“uid”: “13289164”,
“platinum”: 0,
“crbt”: 1,
“crbtMonthly”: 1,
“vrbt”: 0,
“miguTotal”: “227”,
“rechargeUrl”: null,
“musician”: 0,
“vipInterest”: {
“opened”: [{
“id”: 3,
“purviewName”: “xe5xbdxa9xe9x93x83xe5x8cx85xe6x9cx88”,
“purviewDepict”: “xe9xabx98xe6xb8x85xe5xbdxa9xe9x93x83xe5x85x8dxe8xb4xb9xe8xaexa2xe8xb4xadrn| DIYxe5xbdxa9xe9x93x83xe5x85x8dxe8xb4xb9xe4xbdxbfxe7x94xa8”,
“hasOpened”: 1
}],
“noOpened”: [{
“id”: 2,
“purviewName”: “xe7x99xbdxe9x87x91xe4xbcx9axe5x91x98”,
“purviewDepic
4000
t”: “xe9xabx98xe6xb8x85xe3x80x81xe6x97xa0xe6x8dx9fxe6xadx8cxe6x9bxb2xe5x85x8dxe8xb4xb9xe4xb8x8bxe8xbdxbdrn| xe8xb6x85xe6xb8x85MVxe5x85x8dxe8xb4xb9xe8xa7x82xe7x9cx8brn|xe4xb8x93xe4xbaxabxe7x9bxb4xe6x92xadxe5x9cxbaxe6xacxa1xe3x80x81xe7x9bxb4xe6x92xadxe7x89xb9xe6x9dx83rn| xe7xa5xa8xe5x8axa1xe5x8dxa1xe3x80x81xe5x92xaaxe5x92x95xe5x88xb8xe5x85x8dxe8xb4xb9xe9xa2x86rn| xe7x9bxb4xe6x92xadxe4xbaxabxe5x85xa8xe7xa8x8bxe6x97xa0xe5xb9xbfxe5x91x8arn| xe7x9bxb4xe6x92xadxe4xbaxabxe5xbcxb9xe5xb9x95xe7x89xb9xe6x95x88”,
“hasOpened”: 0
}]
}
}
}
解析得到json中uSessionId的值:
if (StringUtils.isNotBlank(json)) { JSONObject jsonObject = JSON.parseObject(json); String user = jsonObject.getString("user"); if (StringUtils.isNotBlank(user)) { JSONObject jsonUser = JSON.parseObject(user); if (jsonUser != null) { String uSessionId = jsonUser.getString("uSessionId"); } } catch (Exception e) { logger.error("定时统计同步贡献值积分到用户中心任务执行异常, obj=" + miguReportActivity); continue; } } } } }
用工具对JSON解析
1,对象
protected User readJsonObject(String jsonData) { // 内置解析方法JSONObject,ctrl+k User bean = null; bean = JsonUtil.parseJsonToBean(jsonData, User.class); return bean; }
2,数组
users = (List<User>) JsonUtil.parseJsonToList(jsonData, new TypeToken<List<User>>(){}.getType());
3,综合
protected List<Future> readJsonObjArr(String jsonData) { // 内置解析方法JSONObject List<Future> futures = new ArrayList<Future>(); String result = JsonUtil.getFieldValue(jsonData, "result"); String future = JsonUtil.getFieldValue(result, "future"); JSONArray jsonArray; try { jsonArray = new JSONArray(future); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String xdate = jsonObject.getString("date"); String xtemperature = jsonObject.getString("temperature"); String xweather = jsonObject.getString("weather"); String xweek = jsonObject.getString("week"); String xwind = jsonObject.getString("wind"); Future f = new Future(xdate, xtemperature, xweather, xweek, xwind); futures.add(f); } } catch (Exception e) { e.printStackTrace(); } return futures; }
相关文章推荐
- 解析xml与json的总结
- vba 解析JSON/XML总结笔记,以GoogleMap/OpenstreetMap-Nominatim为例
- C#解析JSON字符串总结
- 解析json的总结方法
- Java解析Json格式数据的小总结
- 第一行代码总结:10网络:10.4解析JSON格式数据10.4.2使用GSON
- android json解析使用总结(一)
- json编码无法解析问题总结
- java解析json字符串方法总结
- AFNetworking 使用总结 (用法+JSON解析
- AFNetworking 使用总结 (用法+JSON解析)
- AFNetworking 使用总结 (用法+JSON解析)
- IOS学习之AFNetworking 使用总结 (用法+JSON解析)
- 各种iOS JSON解析总结
- JSON总结及其解析(for Java) .
- C# 解析JSON方法总结
- AFNetworking 使用总结 (用法+JSON解析)
- Android Json解析与总结
- Android总结之json解析(FastJson Gson 对比)[申明:来源于网络]
- JSON 字符串解析技巧总结