学习JSONObject/JSONArray

前端之家收集整理的这篇文章主要介绍了学习JSONObject/JSONArray前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

学习JSONObject/JSONArray

@H_301_3@package test; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /*关键词: * 1.JSONObject:json对象,{key:value} * 2.JSONArray:json数组,[{key:value},{key:value}] * 3.Json对象中添加的是键值对,JSONArray中添加的是Json对象 * 4.JSONObject与Map:map和json都是键值对,不同的是map中的每一对键值对是用等号对应起来的,而json中的每一对键值对是用冒号对应起来的. */ public class TestJson { public static void main(String[] args) { String arrays="[{name1:{name2:{name3:'value1',name4:'value2'}}},{}]"; JSONArray getJSONArray=JSONArray.fromObject(arrays);//将结果转化成JSONArray对象 JSONObject getJsonObj =getJSONArray.getJSONObject(0);//获取json数组中的第一项 String result=getJsonObj.getJSONObject("name1").getJSONObject("name2").getString("name4"); System.out.println("取出name4的值: "+result); JSONObject jsonobject=new JSONObject();//json对象 jsonobject.put("key","value"); JSONArray jsonarray=new JSONArray();//json数组 jsonarray.add(jsonobject); //其实json就是一种特殊形式的map。 Map<String,String> strmap=new JSONObject(); } }

猜你在找的Json相关文章