美文网首页Code
Json解析方式

Json解析方式

作者: Clannad_汐 | 来源:发表于2017-07-27 13:53 被阅读0次
1.传统的JSON解析

1.1 生成Json

public static String createJsonString(String key, Object value) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(key, value);
        return jsonObject.toString();
}

1.2 解析Json

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.android.myjson.domain.Person;

/**
 * 完成对json数据的解析
 * 
 */
public class JsonTools {


    public static Person getPerson(String key, String jsonString) {
        Person person = new Person();
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONObject personObject = jsonObject.getJSONObject("person");
            person.setId(personObject.getInt("id"));
            person.setName(personObject.getString("name"));
            person.setAddress(personObject.getString("address"));
        } catch (Exception e) {
            // TODO: handle exception
        }
        return person;
    }

    public static List getPersons(String key, String jsonString) {
        List list = new ArrayList();
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            // 返回json的数组
            JSONArray jsonArray = jsonObject.getJSONArray(key);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                Person person = new Person();
                person.setId(jsonObject2.getInt("id"));
                person.setName(jsonObject2.getString("name"));
                person.setAddress(jsonObject2.getString("address"));
                list.add(person);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return list;
    }

    public static List getList(String key, String jsonString) {
        List list = new ArrayList();
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray jsonArray = jsonObject.getJSONArray(key);
            for (int i = 0; i < jsonArray.length(); i++) {
                String msg = jsonArray.getString(i);
                list.add(msg);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return list;
    }

    public static List> listKeyMaps(String key,
            String jsonString) {
        List> list = new ArrayList>();
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray jsonArray = jsonObject.getJSONArray(key);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                Map map = new HashMap();
                Iterator iterator = jsonObject2.keys();
                while (iterator.hasNext()) {
                    String json_key = iterator.next();
                    Object json_value = jsonObject2.get(json_key);
                    if (json_value == null) {
                        json_value = "";
                    }
                    map.put(json_key, json_value);
                }
                list.add(map);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return list;
    }
}
2.Gson解析Json

2.1生成Json

import com.google.gson.Gson;

public class JsonUtils {

    public static String createJsonObject(Object obj) {
        Gson gson = new Gson();
        String str = gson.toJson(obj);
        return str;

    }
}

2.2解析Json

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

;
public class GsonTools {

    public GsonTools() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param 
     * @param jsonString
     * @param cls
     * @return
     */
    public static  T getPerson(String jsonString, Class cls) {
        T t = null;
        try {
            Gson gson = new Gson();
            t = gson.fromJson(jsonString, cls);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return t;
    }

    /**
     * 使用Gson进行解析 List
     * 
     * @param 
     * @param jsonString
     * @param cls
     * @return
     */
    public static  List getPersons(String jsonString, Class cls) {
        List list = new ArrayList();
        try {
            Gson gson = new Gson();
            list = gson.fromJson(jsonString, new TypeToken>() {
            }.getType());
        } catch (Exception e) {
        }
        return list;
    }

    /**
     * @param jsonString
     * @return
     */
    public static List getList(String jsonString) {
        List list = new ArrayList();
        try {
            Gson gson = new Gson();
            list = gson.fromJson(jsonString, new TypeToken>() {
            }.getType());
        } catch (Exception e) {
            // TODO: handle exception
        }
        return list;
    }

    public static List> listKeyMaps(String jsonString) {
        List> list = new ArrayList>();
        try {
            Gson gson = new Gson();
            list = gson.fromJson(jsonString,
                    new TypeToken>>() {
                    }.getType());
        } catch (Exception e) {
            // TODO: handle exception
        }
        return list;
    }
}
3.FastJson解析Json

3.1 FastJson解析Json

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

public class JsonTool {

    public static  T getPerson(String jsonstring, Class cls) {
        T t = null;
        try {
            t = JSON.parseObject(jsonstring, cls);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return t;
    }

    public static  List getPersonList(String jsonstring, Class cls) {
        List list = new ArrayList();
        try {
            list = JSON.parseArray(jsonstring, cls);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return list;
    }

    public static  List> getPersonListMap1(
            String jsonstring) {
        List> list = new ArrayList>();
        try {
            list = JSON.parseObject(jsonstring,
                    new TypeReference>>() {
                    }.getType());

        } catch (Exception e) {
            // TODO: handle exception
        }

        return list;
    }
}

注:在这三种解析方式中FastJson是效率最高的,推荐使用

相关文章

  • Android Gson官方推荐的json解析方式

    导航 XML的三种解析方式 json全面解析和使用 Gson官方推荐的json解析方式 Gson Gson解析是g...

  • Android XML解析的三种方式

    导航 XML的三种解析方式 json全面解析和使用 Gson官方推荐的json解析方式 三种解析XML方法的比较 ...

  • Android 之json全面解析和使用

    导航 XML的三种解析方式 json全面解析和使用 Gson官方推荐的json解析方式 本节引言: 相信大家肯定对...

  • 数据解析

    XML数据格式解析 pull解析方式 sax解析方式 JSON 数据格式解析 解析代码很简单,但是还要有APP类,...

  • JSON.parse()和eval()的区别

    json格式非常受欢迎,而解析json的方式通常用JSON.parse()但是eval()方法也可以解析,这两者之...

  • json数据解析

    json 是现在非常常见的数据传递方式, go本身也集成了json的生成和解析 引入包 解析 定义 json 对应...

  • JSON 转含有泛型属性的对象

    返回的json数据里嵌套了对象,接收对象里嵌套了泛型。 解析方式: 1.JSON.parseObject();解析...

  • Egret白鹭Protobuf 静态解析使用

    protobufjs有3中使用方式,实时解析方式、json解析方式、静态代码方式(事先导出proto的js文件)。...

  • JSON 解析

    JSON解析三种方式: 下面简单说明一下三种解析方式如何使用 公共代码 json文档内容: 为了方便,在java直...

  • Json解析方式

    1.传统的JSON解析 1.1 生成Json 1.2 解析Json 2.Gson解析Json 2.1生成Json ...

网友评论

    本文标题:Json解析方式

    本文链接:https://www.haomeiwen.com/subject/unszkxtx.html