标签:
JSON的数据格式
package com.diysoul.json.fastjson; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; public class JsonUtil { public JsonUtil() { } public static String createJson(Object object) { try { return JSONObject.toJSONString(object); } catch (Exception e) { } return ""; } public static String createJson(List<Object> object) { try { return JSONObject.toJSONString(object); } catch (Exception e) { } return ""; } public static <T> T getObject(String jsonString, Class<T> klass) { try { T Object = (T) JSONObject.parseObject(jsonString, klass); return Object; } catch (Exception e) { return null; } } public static <T> List<T> getObjects(String jsonString, Class<T> klass) { try { List<T> list = JSONObject.parseArray(jsonString, klass); return list; } catch (Exception e) { return null; } } public static <T> List<T> getObjects(String jsonString) { try { TypeReference<List<T>> typeReference = new TypeReference<List<T>>() { }; List<T> list = JSONObject.parseObject(jsonString, typeReference); return list; } catch (Exception e) { return null; } } public static <K, V> List<Map<K, V>> getMaps(String jsonString) { try { TypeReference<List<Map<K, V>>> typeReference = new TypeReference<List<Map<K, V>>>() { }; List<Map<K, V>> list = JSONObject.parseObject(jsonString, typeReference); return list; } catch (Exception e) { return null; } } }
package com.android.mygson.gson; 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 <T> * @param jsonString * @param cls * @return */ public static <T> T getPerson(String jsonString, Class<T> cls) { T t = null; try { Gson gson = new Gson(); t = gson.fromJson(jsonString, cls); } catch (Exception e) { // TODO: handle exception } return t; } /** * 使用Gson进行解析 List<Person> * * @param <T> * @param jsonString * @param cls * @return */ public static <T> List<T> getPersons(String jsonString, Class<T> cls) { List<T> list = new ArrayList<T>(); try { Gson gson = new Gson(); list = gson.fromJson(jsonString, new TypeToken<List<T>>() { }.getType()); } catch (Exception e) { } return list; } /** * @param jsonString * @return */ public static List<String> getList(String jsonString) { List<String> list = new ArrayList<String>(); try { Gson gson = new Gson(); list = gson.fromJson(jsonString, new TypeToken<List<String>>() { }.getType()); } catch (Exception e) { // TODO: handle exception } return list; } public static List<Map<String, Object>> listKeyMaps(String jsonString) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); try { Gson gson = new Gson(); list = gson.fromJson(jsonString, new TypeToken<List<Map<String, Object>>>() { }.getType()); } catch (Exception e) { // TODO: handle exception } return list; }
/**
* @param object
* 是对解析的集合的类型
* @return
*/
public static String createJsonString(Object value) {
Gson gson = new Gson();
String str = gson.toJson(value);
return str;
}
}
package com.android.myjson.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数据的解析 * * @author jack * */ public class JsonTools { public JsonTools() { // TODO Auto-generated constructor stub } 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<Person> getPersons(String key, String jsonString) { List<Person> list = new ArrayList<Person>(); 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<String> getList(String key, String jsonString) { List<String> list = new ArrayList<String>(); 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<Map<String, Object>> listKeyMaps(String key, String jsonString) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 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<String, Object> map = new HashMap<String, Object>(); Iterator<String> 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; }
/**
* @param key
* 表示json字符串的头信息
* @param object
* 是对解析的集合的类型
* @return
*/
public static String createJsonString(String key, Object value) {
JSONObject jsonObject = new JSONObject();
jsonObject.put(key, value);
return jsonObject.toString();
} }
标签:
原文地址:http://www.cnblogs.com/diysoul/p/5515865.html