[b]JSONObject、JSONArray[/b]
[img]http://dl2.iteye.com/upload/attachment/0120/0407/52d17204-3540-3f1a-8454-658301078ecf.png[/img]
1.从外到里看,例子中就是一个数组,数组里面是两个json格式的字符串。
2.工作中需要取出name4的值,你们会怎么取呢?。最初我都想过字符串截取,那时还不了解JSONArray,现在知道了,
取出来也就相当容易了。
[b]取出name4值过程步骤:[/b]
1,将以上字符串转换为JSONArray对象;
2,取出对象的第一项,JSONObject对象;
3,取出name1的值JSONObject对象;
4,取出name2的值JSONObject对象;
5,取出name4的值value2。
示例中json数组格式的字符串可以通过方法直接转换为JSONArray的格式:JSONArray.fromObject(String)
JSONArray getJsonArray=JSONArray.fromObject(arrayStr);//将结果转换成JSONArray对象的形式
JSONObject getJsonObj = getJsonArray.getJSONObject(0);//获取json数组中的第一项
String result=getJsonObj.getJSONObject("name1").getJSONObject("name2").getJSONObject("name4");
[b]JSONObject[/b]
json对象,就是一个键对应一个值,使用的是大括号{ },如:{key:value}
[b]JSONArray[/b]
1.json数组,使用中括号[ ],只不过数组里面的项也是json键值对格式的
2.Json对象中添加的是键值对,JSONArray中添加的是Json对象
[b]JSONObject与Map[/b]
Map map和json都是键值对,不同的是map中键值对中间用等号分开,json中键值对中间用冒号分开。
其实json就是一种特殊形式的map。
Map<String,String> strmap=new JSONObject();
参考原文:[url]http://blog.csdn.net/lishuangzhe7047/article/details/28880009[/url]
[img]http://dl2.iteye.com/upload/attachment/0120/0407/52d17204-3540-3f1a-8454-658301078ecf.png[/img]
1.从外到里看,例子中就是一个数组,数组里面是两个json格式的字符串。
2.工作中需要取出name4的值,你们会怎么取呢?。最初我都想过字符串截取,那时还不了解JSONArray,现在知道了,
取出来也就相当容易了。
[b]取出name4值过程步骤:[/b]
1,将以上字符串转换为JSONArray对象;
2,取出对象的第一项,JSONObject对象;
3,取出name1的值JSONObject对象;
4,取出name2的值JSONObject对象;
5,取出name4的值value2。
示例中json数组格式的字符串可以通过方法直接转换为JSONArray的格式:JSONArray.fromObject(String)
JSONArray getJsonArray=JSONArray.fromObject(arrayStr);//将结果转换成JSONArray对象的形式
JSONObject getJsonObj = getJsonArray.getJSONObject(0);//获取json数组中的第一项
String result=getJsonObj.getJSONObject("name1").getJSONObject("name2").getJSONObject("name4");
[b]JSONObject[/b]
json对象,就是一个键对应一个值,使用的是大括号{ },如:{key:value}
[b]JSONArray[/b]
1.json数组,使用中括号[ ],只不过数组里面的项也是json键值对格式的
2.Json对象中添加的是键值对,JSONArray中添加的是Json对象
[b]JSONObject与Map[/b]
Map map和json都是键值对,不同的是map中键值对中间用等号分开,json中键值对中间用冒号分开。
其实json就是一种特殊形式的map。
Map<String,String> strmap=new JSONObject();
参考原文:[url]http://blog.csdn.net/lishuangzhe7047/article/details/28880009[/url]