Json的全称:JavaScript Object Notation
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
/**
* 语义结果的Json格式解析
* @param json
* @return
*//*
public static String parseUnderstandResult(String json) {
StringBuffer ret = new StringBuffer();
try {
JSONTokener tokener = new JSONTokener(json);
JSONObject joResult = new JSONObject(tokener);
//服务的细分操作编码,各业务服务自定义
ret.append("【操作名称】" + joResult.getString("operation") + "\n");//必须
//上下文信息,客户端需要将该字段结果传给下一次请求的history字段
if(joResult.has("history"))
ret.append("【history字段】" + joResult.getString("history") + "\n");//选填
//错误信息
if(joResult.has("error")){
JSONObject error=joResult.getJSONObject("error");//选填
ret.append("【code字段】" + error.getString("code") + "\n");//选填——必填
//ret.append("【message字段】" + error.getString("message") + "\n");//选填——选填
}
//在存在多个候选结果时, 用于提供更多的结果描述
if(joResult.has("moreResults")){
JSONArray moreResults=joResult.getJSONArray("moreResults");//选填
for (int i = 0; i <moreResults.length(); i++) {
JSONObject ret2=moreResults.getJSONObject(i);
ret.append("【*****moreResults******】"+ "\n" + parseUnderstandResult(moreResults.get(i).toString()) + "\n");//选填——必填
}
}
} catch (Exception e) {
e.printStackTrace();
ret.append("没有匹配结果.");
}
return ret.toString();
}
public class GsonTools {
public GsonTools() {
// TODO Auto-generated constructor stub
}
//对象转json格式字符串,支持任意格式的class\list\map
public static String createGsonString(Object object) {
Gson gson = new Gson();
String gsonString = gson.toJson(object);
return gsonString;
}
public static <T> T changeGsonToBean(String gsonString, Class<T> cls) {
Gson gson = new Gson();
T t = gson.fromJson(gsonString, cls);
return t;
}
public static <T> List<T> changeGsonToList(String gsonString, Class<T> cls) {
Gson gson = new Gson();
List<T> list_person = gson.fromJson(gsonString,
new TypeToken<List<T>>() {
}.getType());
return list_person;
}
//将json字符串还原为 list<map>格式
public static List<Map<String, Object>> changeGsonToListMaps(String gsonString) {
List<Map<String, Object>> list = null;
Gson gson = new Gson();
list = gson.fromJson(gsonString,
new TypeToken<List<Map<String, Object>>>() {
}.getType());
return list;
}
/** * @param args */
public static void main(String[] args) {
// TODO Auto-generated method stub
// Person person1 = new Person("张三1", 24, "北京1");
// Person person2 = new Person("张三2", 23, "北京2");
// List<Person> list = new ArrayList<Person>();
// list.add(person1);
// list.add(person2);
//
// String gsonString = createGsonString(list);
// System.out.println(gsonString);
// Gson gson = new Gson();
// List<Person> list_person = changeGsonToList(gsonString,Person.class);
// System.out.println(list_person.toString());
// Person pp = changeGsonToBean(gsonString,Person.class);
// System.out.println(pp);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "jack");
map.put("age", 23);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("name", "rose");
map2.put("age", 24);
list.add(map);
list.add(map2);
String gsonString = createGsonString(list);
System.out.println(gsonString);
List<Map<String, Object>> list2 = changeGsonToListMaps(gsonString);
System.out.println(list2.toString());
}
}
FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:
速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser。包括自称最快的JackJson。
功能强大,完全支持Java Bean、集合、Map、日期、Enum,支持范型,支持自省。
无依赖,能够直接运行在Java SE 5.0以上版本
支持Android。
开源 (Apache 2.0)
package com.android.myfastjson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.asm.Type;
import com.alibaba.fastjson.util.TypeUtils;
public class FastJsonTools {
public FastJsonTools() {
// TODO Auto-generated constructor stub
}
public static String createJsonString(Object object) {
String jsonString = JSON.toJSONString(object);
return jsonString;
}
public static <T> T createJsonBean(String jsonString, Class<T> cls) {
T t = JSON.parseObject(jsonString, cls);
return t;
}
public static <T> List<T> createJsonToListBean(String jsonString,
Class<T> cls) {
List<T> list = null;
list = JSON.parseArray(jsonString, cls);
return list;
}
/** * @param jsonString * @return */
public static List<Map<String, Object>> createJsonToListMap(
String jsonString) {
List<Map<String, Object>> list2 = JSON.parseObject(jsonString,
new TypeReference<List<Map<String, Object>>>() {
});
return list2;
}
/** * @param args */
public static void main(String[] args) {
// TODO Auto-generated method stub
// Person person1 = new Person("张三1", 24, "北京1");
// Person person2 = new Person("张三2", 23, "北京2");
// List<Person> list = new ArrayList<Person>();
// list.add(person1);
// list.add(person2);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "jack");
map.put("age", 23);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("name", "rose");
map2.put("age", 24);
list.add(map);
list.add(map2);
String jsonString = JSON.toJSONString(list);
System.out.println(jsonString);
// JSON.parseArray(arg0, arg1)
List<Map<String, Object>> list2 = JSON.parseObject(jsonString,
new TypeReference<List<Map<String, Object>>>() {
});
// List<Person> lists = JSON.parseArray(arg0, arg1);
System.out.println(list2.toString());
}
}