json解析之自带方法
来源:互联网 发布:犀牛软件自学视频教程 编辑:程序博客网 时间:2024/05/01 16:00
json解析学习了两种方法一种是自带的json相关的类来解析,一种是google提供的gson方法;
下面是自带的方法:
首先是handler类:
package com.example.administrator.test.json;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;public class JsonHandler { /** * 这个方法不是解析json数据的,而是生成json数据格式的方法,一般用在服务器端? * * @param key * @param value * @return json的string格式 * @throws Exception */ public static String createJsonString(String key, Object value) throws Exception { JSONObject jsonObject = new JSONObject(); jsonObject.put(key, value); return jsonObject.toString(); } /** * 以下的方法才是解析json的 * 这里是解析单个jsonObject的方法 * * @param key * @param jsonString * @return 装载数据的person对象 * @throws JSONException */ public static Person getPerson(String key, String jsonString) throws JSONException { Person person = new Person(-1, "null", "null", -1); JSONObject jsonObject = new JSONObject(jsonString); JSONObject personObject = jsonObject.getJSONObject(key); person.setId(personObject.getInt("id")); person.setSex(personObject.getString("sex")); person.setName(personObject.getString("name")); person.setAge(personObject.getInt("age")); return person; } /** * 这里是解析jsonArray的方法 * * @param key * @param jsonString * @return 装载数据的list<person>对象 * @throws Exception */ public static List<Person> getPersons(String key, String jsonString) throws Exception { List<Person> list = new ArrayList<>(); JSONObject jsonObject = new JSONObject(jsonString); JSONArray jsonArray = jsonObject.getJSONArray(key); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject1 = jsonArray.getJSONObject(i); Person person = new Person(-1, "null", "null", -1); person.setId(jsonObject1.getInt("id")); person.setSex(jsonObject1.getString("sex")); person.setName(jsonObject1.getString("name")); person.setAge(jsonObject1.getInt("age")); list.add(person); } return list; } /** * 这里也是解析jsonArray的方法 * * @param key * @param jsonString * @return 装载数据的list<map>对象 * @throws Exception */ public static List<Map<String, Object>> getMaps(String key, String jsonString) throws Exception { List<Map<String, Object>> list = new ArrayList<>(); JSONObject jsonObject = new JSONObject(jsonString); JSONArray jsonArray = jsonObject.getJSONArray(key); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject1 = jsonArray.getJSONObject(i); Map<String, Object> map = new HashMap<>(); Iterator<String> iterator = jsonObject1.keys(); while (iterator.hasNext()) { String json_key = iterator.next(); Object json_value = jsonObject1.get(json_key); if (json_value == null) json_value = ""; map.put(json_key, json_value); } list.add(map); } return list; }}下面是test方法的代码:
/** * 将数据转换成json格式 */ public void testCreateJsonString() { String jsonString = "no data"; String jsonString2 = "no data"; String jsonString3 = "no data"; //需要转换成json格式的数据的对象,这里是person来装载数据 Person person = new Person(1, "boy", "jack", 22); Person person2 = new Person(2, "girl", "rose", 20); List<Person> list = new ArrayList<>(); list.add(person); list.add(person2); //需要转换成json格式的数据的对象,这里是map来装载数据 List<Map<String, Object>> list2 = new ArrayList<>(); Map<String, Object> map = new HashMap<>(); map.put("id", 1); map.put("sex", "boy"); map.put("name", "jack"); map.put("age", 22); Map<String, Object> map2 = new HashMap<>(); map2.put("id", 2); map2.put("sex", "girl"); map2.put("name", "rose"); map2.put("age", 20); list2.add(map); list2.add(map2); //注意下面的生成json的各种方式 try { jsonString = JsonHandler.createJsonString("person", person); System.out.println("jsonString-------------->" + jsonString); jsonString2 = JsonHandler.createJsonString("person", list); System.out.println("jsonListString2-------------->" + jsonString2); jsonString3 = JsonHandler.createJsonString("person", list2); System.out.println("jsonListString3-------------->" + jsonString3); } catch (Exception e) { e.printStackTrace(); } } /** * 解析json数据 */ public void testGetJson() { File file = new File(Environment.getExternalStorageDirectory(), "data.json"); FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int len; if ((len = fileInputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, len); } String jsonString = new String(bytes); //由于自己写的json是array格式的,对于单个jsonObject不适用,所以注释了// Person person3 = JsonHandler.getPerson("person", jsonString);// System.out.println("person3------------------>" + person3.toString()); List<Person> list1 = JsonHandler.getPersons("person", jsonString); System.out.println("list1------------------>" + list1.toString()); List<Map<String, Object>> list3 = JsonHandler.getMaps("person", jsonString); System.out.println("list3------------------>" + list3.toString()); } catch (Exception e) { e.printStackTrace(); } finally { try { if (fileInputStream != null) fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
0 0
- json解析之自带方法
- iOS 自带方法解析json
- IOS自带json解析之生成json对象
- JSON复杂文档解析 Android自带JSONObject,JSONArray方法
- 用Android自带方法解析json类型文件
- 自带JSON解析工具
- json 利用自带json解析
- JSON介绍及Android最全面解析方法(Gson、AS自带org.son、Jackson解析)
- JSON介绍及Android最全面解析方法(Gson、AS自带org.son、Jackson解析)
- IOS5.0 自带JSON解析
- 发送请求JSON解析自带
- NSJSONSerialization iOS自带解析json
- 【Android】解析Json对象,用自带json解析器
- IOS中四种解析JSON的方法(IOS5自带支持方法、JSONKit、SBJson、TouchJson)
- Android开发:JSON简介及最全面解析方法(Gson、AS自带org.json、Jackson解析)
- Android开发:JSON简介及最全面解析方法(Gson、AS自带org.json、Jackson解析)
- Android开发:JSON简介及最全面解析方法(Gson、AS自带org.json、Jackson解析)
- 用安卓自带的原生方法解析从webservice获取的json数据
- CoreData NSPredicate在CoreData中的使用
- 604课件
- obeject分析与例子
- HTML原则指南
- PROFIBUS通讯中终端电阻的TIPS
- json解析之自带方法
- Java的特点
- 初学Android项目:开发电子市场<第三天>
- CString转const char*
- poj 1860
- IOS 整体框架类图
- 第十一周实践项目1—验证算法(4)哈夫曼编码的算法验证
- JavaScript闭包之Fibonacci数列
- mysql主从复制配置管理