[Android]JsonObject解析
2015-07-22 12:34
489 查看
android和服务器进行交互的时候往往会有数据的传输,而数据中有一种类型就是Json型,这两天在研究API接口的问题,服务器返回的数据类型都是Json型的。例如:
1.接收到的json字符串分为两种一种为array型,一种为Object型。下面就是Object型(嵌套型):
{"errNum":0,"errMsg":"success","retData":{"city":"\u5317\u4eac","pinyin":"beijing","citycode":"101010100","date":"15-07-21","time":"11:00","postCode":"100000","longitude":116.391,"latitude":39.904,"altitude":"33","weather":"\u96f7\u9635\u96e8","temp":"28","l_tmp":"22","h_tmp":"28","WD":"\u65e0\u6301\u7eed\u98ce\u5411","WS":"\u5fae\u98ce(<10m\/h)","sunrise":"05:02","sunset":"19:38"}}
2.提取其中的value值:
(代码已测试)
1.接收到的json字符串分为两种一种为array型,一种为Object型。下面就是Object型(嵌套型):
{"errNum":0,"errMsg":"success","retData":{"city":"\u5317\u4eac","pinyin":"beijing","citycode":"101010100","date":"15-07-21","time":"11:00","postCode":"100000","longitude":116.391,"latitude":39.904,"altitude":"33","weather":"\u96f7\u9635\u96e8","temp":"28","l_tmp":"22","h_tmp":"28","WD":"\u65e0\u6301\u7eed\u98ce\u5411","WS":"\u5fae\u98ce(<10m\/h)","sunrise":"05:02","sunset":"19:38"}}
2.提取其中的value值:
(代码已测试)
String response = (String) msg.obj; // Json字符串解析 String errMsg = null; int errNum = 2; try { JSONObject jsonObject = new JSONObject(response.toString()); errMsg = jsonObject.getString("errMsg"); errNum = jsonObject.getInt("errNum"); } catch (JSONException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } JSONObject jsonObject1 = null; try { jsonObject1 = new JSONObject(response.toString()) .getJSONObject("retData"); } catch (JSONException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } String city = null; String date = null; String time = null; String weather = null; String temp = null; String l_tmp = null; String h_tmp = null; String WD = null; String WS = null; String sunrise = null; String sunset = null; try { city = jsonObject1.getString("city"); date = jsonObject1.getString("date"); time = jsonObject1.getString("time"); weather = jsonObject1.getString("weather"); temp = jsonObject1.getString("temp"); l_tmp = jsonObject1.getString("l_tmp"); h_tmp = jsonObject1.getString("h_tmp"); WD = jsonObject1.getString("WD"); WS = jsonObject1.getString("WS"); sunrise = jsonObject1.getString("sunrise"); sunset = jsonObject1.getString("sunset"); } catch (JSONException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); }
相关文章推荐
- Microsoft VBScript 运行时错误 (0x800A0046) 没有权限: 'CreateObject' 解决方法
- Objective-C Runtime 运行时之四:Method Swizzling
- Scripting.FileSystemObject控件的用法
- Is there a complete list of PSPROJECTITEM.OBJECTTYPE Values for PeopleTools 8.51?
- Object c assign/retain/copy
- window.ActiveXObject使用说明
- 【iOS开发之Objective-C】初始化对象
- Objective_C中常见的集合类
- Objective-C对象和Core Foundation 对象
- Objective-C中一些 值得程序员注意的地方
- Hibernate中根据实体查询报错Ljava.lang.Object; cannot be。。。
- java之顶级父类Object
- Object-C【01】OC语言简述
- 局域网WEB方式实现TTS实时语音合成
- JAVA DAO(Data Access Object)的个人总结
- Objective-C学习笔记之结构体以及指针
- Objective-C语言--Key-Value Observing Programming Guide
- 在framework中打包xib
- Xcode中添加pch文件
- Objective-C 内存管理(2)