fastjson介绍与使用
pom.xml文件中加入依赖依赖: <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.0.4</version> </dependency>
Spring中的配置(用来支持解析@ResponseBody)
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json</value> </list> </property> </bean> <bean class=" org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="fastJsonHttpMessageConverter"/> </list> </property> </bean>
对象转json
JSON.toJSONString(Bean, true);// 后面一个参数是是否保持顺序
json字符转对象
public static HashMap<String, Object> deserilizableForMapFromFile(String InputfilePathName) throws IOException { Map<String, Object> map = JSON.parseObject(InputfilePathName, new TypeReference<Map<String, Object>>() { }); return (HashMap<String, Object>) map; } public static ApiData deserilizableForApidata(String str) { ApiData apiData = JSON.parseObject(str, ApiData.class); return apiData; }
其他用法:
1.String转JSONObject
String jsonMessage = "{\"语文\":\"88\",\"数学\":\"78\",\"计算机\":\"99\"}";
JSONObject myJson = JSONObject.fromObject(jsonMessage);
2.String转JSONArray
String jsonMessage = "[{'num':'成绩', '外语':88, '历史':65, '地理':99, 'object':{'aaa':'1111','bbb':'2222','cccc':'3333'}}," +
"{'num':'兴趣', '外语':28, '历史':45, '地理':19, 'object':{'aaa':'11a11','bbb':'2222','cccc':'3333'}}," +
"{'num':'爱好', '外语':48, '历史':62, '地理':39, 'object':{'aaa':'11c11','bbb':'2222','cccc':'3333'}}]";
JSONArray myJsonArray = JSONArray.fromObject(jsonMessage);
System.out.println(myJsonArray);
3.String转Map
(1)
String jsonMessage = "{\"语文\":\"88\",\"数学\":\"78\",\"计算机\":\"99\"}";
JSONObject myJson = JSONObject.fromObject(jsonMessage);
Map m = myJson;
(2) (需要引入jackjson的core、databind、annotations三个jar包 )
String json = "{\"PayPal key2\":\"PayPal value2\",\"PayPal key1\":\"PayPal value1\",\"PayPal key3\":\"PayPalvalue3\"}";
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> m = mapper.readValue(json, Map.class);
(3)(特殊格式的String)
String a ="{se=2016, format=xml, at=en co=3}";
a = a.substring(1, a.length()-1);
Map docType = new HashMap();
Java.util.StringTokenizer items;
for(StringTokenizer entrys = new StringTokenizer(a, ", ");entrys.hasMoreTokens();
docType.put(items.nextToken(), items.hasMoreTokens() ? ((Object) (items.nextToken())) : null)){
items = new StringTokenizer(entrys.nextToken(), "=");
}
4.JSONObject、JSONArray,Map转String
JSONObject——String:
System.out.println(myJsonObject);//可直接输出JSONObject的内容
myJsonObject.toString();
JSONArray——String:
System.out.println(myJsonArray);//可直接输出myJsonArray的内容
myJsonArray.toString();
Map——String:
System.out.println(map);//可直接输出map的内容
map.toString();
5.JSONObject转JSONArray
6.JSONObject转Map
JSONObject myJson = JSONObject.fromObject(jsonString);
Map m = myJson;
7.JSONArray转JSONObject
for(int i=0 ; i < myJsonArray.length() ;i++)
{
//获取每一个JsonObject对象
JSONObject myjObject = myJsonArray.getJSONObject(i);
}
8.JSONArray转Map
9.Map转JSONObject
JSONObject json = JSONObject.fromObject( map );
10.Map转JSONArray
JSONArray.fromObject(map);
11.List转JSONArray
JSONArray jsonArray2 = JSONArray.fromObject( list );
12.JSONArray转List
List<Map<String,Object>> mapListJson = (List)jsonArray;