一。JavaProject中org.json解析JSON
1.JSON的org.son-api下载
一般搜索jar使用即可。
1)JSON网址
2)JSON的java解析org.json-api网址
http://www.json.org/java/index.html
3)JSON的org.json-api源码地址
https://github.com/douglascrockford/JSON-java
本例下载日期:2014-11-17
2.在JavaProject中使用
1)创建org.json包
2)从下载的zip包中解压源码
如果是使用jar,当然没这么麻烦。
3.使用org.json解析JSON
1 2 3 4 5 |
import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; |
1)String <---> JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/** String转JSON(JSONArray) **/ // 最外围由 [] 包裹 String jsonStr1 = "[{ " + " \"user3\" : \"翠花\", " + " \"pswd3\" : \"cuihua123\", " + " \"body\": { \"chest\": 68, \"waist\": 65, \"hip\": 68 }" + "}]"; JSONArray jarray2 = new JSONArray(jsonStr1); System.out.println(jarray2.getJSONObject(0)); /** String 转换成为 JSON(JSONObject) 对象 */ // 最外围由 {} 包裹 String jsonStr2 = "{ " + " \"user4\" :\"凤姐\", " + " \"pswd4\" :\"fengjie123\", " + " \"body\": { \"chest\":65, \"waist\":63, \"hip\":66 }" + "}"; JSONObject jsonObj1 = new JSONObject(jsonStr2); // 根据属性名称获取String型数据; String user4 = jsonObj1.getString("user4"); String pswd4 = jsonObj1.getString("pswd4"); // 根据属性名获取JSONObject类 JSONObject body = jsonObj1.getJSONObject("body"); // 根据属性名称获取int型数据; int chest = body.getInt("chest"); int waist = body.getInt("waist"); int hip = body.getInt("hip"); |
2)集合 <---> JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/** List集合转JSON(JSONArray) **/ Map<String, String> param1 = new HashMap<String, String>(); param1.put("user1", "小白"); param1.put("pswd1", "xiaobai123"); Map<String, String> param2 = new HashMap<String, String>(); param2.put("user2", "小黑"); param2.put("pswd2", "xiaohei123"); // JSONArray initial value should be a string or collection or array. List<Object> params = new ArrayList<Object>(); params.add(param1); params.add(param2); // 声明JSONArray对象并输入JSON字符串 JSONArray jarray1 = new JSONArray(params); // 除了构造,还有put方法 System.out.println(jarray1.toString()); /** Map 转 JSON **/ Map<String, String> map = new HashMap<String, String>(); map.put("user5", "小五"); map.put("pswd5", "xiaowu123"); JSONArray jarray3 = new JSONArray(); jarray3.put(map); // 可以put进map,或者list System.out.println(jarray3.toString()); |
3)JavaBean <---> JSON
1 2 3 4 5 6 7 8 9 10 |
/* JavaBean的属性: String username; String password; int chest, waist, hip; */ /** JavaBean 转换成为 JSON(JSONObject) 对象 */ bean.setUsername("小四"); JSONObject jsonObj2 = new JSONObject(bean); System.out.println(jsonObj2.get("username") + ", chest:" + jsonObj2.getInt("chest")); |
二。JavaProject中json-lib解析JSON
1.JSON的json-lib-api下载
http://json-lib.sourceforge.net/
http://sourceforge.net/projects/json-lib/files/
2.在JavaProject中使用
需要其它依赖jar,有些麻烦。
3.使用json-lib解析JSON
1 2 3 4 |
import net.sf.json.JSONArray; import net.sf.json.JSONObject; |
1)String <---> JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// 一般数据 转 JSON(JSONArray) JSONArray jsonArray3 = JSONArray.fromObject("[{'user3':'凤姐'}, {'pswd3':'fengjie123'}]" ); System.out.println(jsonArray3.toString()); // 一般数据 转 JSON(JSONArray) JSONArray jsonArray4 = JSONArray.fromObject("['user4', '凤姐xxx', 'pswd4', 'fengjiexxx123']" ); System.out.println(jsonArray4.toString()); // 数组 转 JSON(JSONArray) String[] strs = new String[]{"小白", "小黑", "小四", "岳父"}; JSONArray jsonArray2 = JSONArray.fromObject(strs); System.out.println(jsonArray2.toString()); |
2)集合 <---> JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// List 集合转 JSON(JSONArray) List list1 = new ArrayList(); list1.add("user1"); list1.add("pswd1"); JSONArray jsonArray1 = JSONArray.fromObject(list1); System.out.println(jsonArray1.toString()); // Map 集合转 JSON(JSONObject) Map<String, String> map = new HashMap<String, String>(); map.put("user2", "芙蓉"); map.put("pswd2", "furong123"); JSONObject jsonObject1 = JSONObject.fromObject(map); System.out.println(jsonObject1.toString()); |
3)JavaBean <---> JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Bean 转 JSON(JSONObject) JSONObject jsonObject2 = JSONObject.fromObject(new JavaBean("拱拱", "gonggong123", 34, 44, 43)); System.out.println(jsonObject2.toString()); // beans 转 JSON List list2 = new ArrayList(); JavaBean bean1 = new JavaBean("逗比", "doubi123", 56, 54, 55); JavaBean bean2 = new JavaBean("屌丝", "diaosi123", 34, 45, 56); list2.add(bean1); list2.add(bean2); JSONArray jsonArray5 = JSONArray.fromObject(list2); System.out.println(jsonArray5.toString()); |
三。AndroidProject中的JSON解析
Android中JSON解析器使用的是org.json。
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/724.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设