Json,Gson,FastJson解析笔记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Json,Gson,FastJson解析笔记相关的知识,希望对你有一定的参考价值。
Json
1.将JavaBean转换成Json对象:
- public static String CreatJsonFromObject(Object key,Object value)
- {
- JSONObject JObj =new JSONObject();
- JObj.put(key, value);
- return JObj.toString();
- }
2.解析Json:
- public static Person parseJsonPerson(String key,String jsonStr)
- {
- System.out.print(key+"-->"+jsonStr);
- Person person =new Person();
- JSONObject jobj;
- try
- {
- jobj = new JSONObject(jsonStr);
- JSONObject personObj =jobj.getJSONObject(key);
- person.setAge(personObj.getInt("age"));
- person.setName(personObj.getString("name"));
- person.setSex(personObj.getString("sex"));
- }
- catch (JSONException e)
- {
- e.printStackTrace();
- }
- return person;
- }
- public static List<Person> parseJsonListPerson(String key,String jsonStr)
- {
- System.out.print(key+"-->"+jsonStr);
- List<Person> list=new ArrayList<Person>();
- try
- {
- JSONObject jobj;
- jobj = new JSONObject(jsonStr);
- JSONArray personArr =jobj.getJSONArray(key);
- for(int i=0;i<personArr.length();i++)
- {
- JSONObject personObj = personArr.getJSONObject(i);
- Person person =new Person();
- person.setAge(personObj.getInt("age"));
- person.setName(personObj.getString("name"));
- person.setSex(personObj.getString("sex"));
- list.add(person);
- }
- }
- catch (JSONException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return list;
- }
- public static List<String> parseJsonListString(String key,String jsonStr)
- {
- System.out.print(key+"-->"+jsonStr);
- List<String> list =new ArrayList<String>();
- try
- {
- JSONObject jobj;
- jobj = new JSONObject(jsonStr);
- JSONArray personArr =jobj.getJSONArray(key);
- for(int i=0;i<personArr.length();i++)
- {
- String personObj = personArr.getString(i);
- list.add(personObj);
- }
- }
- catch (JSONException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return list;
- }
- public static List<Map<String, Object>> parseJsonListMap(String key,String jsonStr)
- {
- System.out.print(key+"-->"+jsonStr);
- List<Map<String, Object>> list =new ArrayList<Map<String,Object>>();
- try
- {
- JSONObject jobj;
- jobj = new JSONObject(jsonStr);
- JSONArray personArr =jobj.getJSONArray(key);
- for(int i=0;i<personArr.length();i++)
- {
- JSONObject personObj = personArr.getJSONObject(i);
- Iterator<String> set =personObj.keys();
- while(set.hasNext())
- {
- String tempKey =set.next();
- Object tempValue =personObj.get(tempKey);
- HashMap<String,Object> map =new HashMap<String,Object>();
- //注意tempValue可能为空
- if(tempValue == null)
- {
- tempValue="";
- }
- map.put(tempKey, tempValue);
- list.add(map);
- }
- }
- }
- catch (JSONException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return list;
- }
Gson:
1.将JavaBean转换成Gson String
- public static String CreatJsonFromObject(Object value)
- {
- Gson gson =new Gson();
- return gson.toJson(value);
- }
2.解析Gson String
- public static <T> T getPerson(String Json, Class<T> cls)
- {
- Gson gson = new Gson();
- T t = gson.fromJson(Json, cls);
- return t;
- }
- public static <T> List<T> getPersons(String Json, Class<T> Type)
- {
- List<T> list = new Gson().fromJson(Json, new TypeToken<List<T>>()
- {
- }.getType());
- return list;
- }
- public static List<String> getString(String Json)
- {
- List<String> retStr = new Gson().fromJson(Json, new TypeToken<List<String>>()
- {
- }.getType());
- return retStr;
- }
- public static List<Map<String, Object>> getListMap(String Json)
- {
- List<Map<String, Object>> retStr = new Gson().fromJson(Json, new TypeToken<List<Map<String, Object>>>()
- {
- }.getType());
- return retStr;
- }
FastJson:
1.将JavaBean转换成FastJson String
服务端利用上述方式实现
2.解析FastJson String
- public static <T> T getPerson(String jsonStr, Class<T> cls)
- {
- T t = JSON.parseObject(jsonStr, cls);
- return t;
- }
- public static <T> List<T> getPersons(String jsonStr, Class<T> cls)
- {
- List<T> list = JSON.parseArray(jsonStr, cls);
- return list;
- }
- /**
- * @param jsonStr
- * @return
- */
- public static List<Map<String, Object>> getListMap(String jsonStr)
- {
- List<Map<String, Object>> list = JSON.parseObject(jsonStr, new TypeReference<List<Map<String, Object>>>()
- {
- });
- return list;
- }
- // from: http://www.voidcn.com/blog/zchlww/article/p-4999460.html
以上是关于Json,Gson,FastJson解析笔记的主要内容,如果未能解决你的问题,请参考以下文章
使用google的Gson库和alibaba的Fastjson库解析json数据的区别