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