Map,HashMap组装Json数据,jackson简单的入门和解析Json例子
package com.ljl.org.domain;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.ObjectMapper;
import com.google.gson.Gson;
import com.ljl.org.entity.Info;
import com.ljl.org.entity.User;
/**
*@Author:liangjilong
*@Date:2013-8-13
*@Email:jilongliang@sina.com
*@Version:Version1.0
*@CopyRight:liangjilong
*@Description:http://wiki.fasterxml.com/JacksonInFiveMinutes#Full_Data_Binding_.28POJO.29_Example
*
*=====================================
* =====================================
* ☆★★建议解析json的有:
* 1)GSON (gson-2.2.4.jar)
* 2)json-lib-2.2.3-jdk15.jar--
* ☆不建议使用的:
* JackSON比较扯淡(毛线)
* 需要的jar
* http://download.csdn.net/detail/jilongliang/5789041
* jackson-all-1.6.2.jar
* jackson-core-asl-1.6.2.jar
* jackson-mapper-asl-1.6.2.jar
* jakarta-oro.jar
* =====================================
* =====================================
*/
public class Jackson1 {
private static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws Exception {
jackSon1();
jackSon2();
}
/**
* 简单的JackSon测试,对比一下,不知道谁抄谁的!Gson和Json-lib-2.2.3-jdk15也有很多相似的
* 使用者两个技术的人都知道、、、、
* ===================================================
* Gson创建一个JsonParser
* JsonParser parser = new JsonParser();
* JsonElement jsonEl = parser.parse(json);
* ===================================================
* JackSon 创建一个JsonParser
* JsonFactory f = new JsonFactory();//实例JSON工程对象
* JsonParser jp = f.createJsonParser(json);
* ===================================================
*/
public static void jackSon1()throws Exception{
String json = "[{\"userName\": \"liangjilong\",\"address\":\"广东阳春\"},{\"userName\": \"liangjilong\",\"address\": \"广东阳春\"}]";
JsonFactory f = new JsonFactory();//实例JSON工程对象
JsonParser jp = f.createJsonParser(json);
jp.nextToken();//下一个JsonToken
while (jp.nextToken() == JsonToken.START_OBJECT) {
User u = mapper.readValue(jp, User.class);//对象化
System.out.println(u.getUserName()+"\t"+u.getAddress());
}
}
/**
* @throws Exception
*/
public static void jackSon2()throws Exception{
/**
* 组装address对象的数据
*/
Map<String, String> address = new HashMap <String, String>();
address.put("province", "广东省");
address.put("city", "广州市");
address.put("district", "天河区");
address.put("street", "棠东");
/**
* 组装users对象的数据
*/
Map<String, String> users = new HashMap <String, String>();
users.put("userName", "liangjilong");
users.put("age", "25");
users.put("tel", "12396878");
users.put("email", "jilongliang@sina.com");
Map<Object, Object> listsObj = new HashMap <Object, Object>();
listsObj.put("address",address);//address节点
listsObj.put("user",users);//users节点
Object obj=listsObj;//转换成对象
Map<Object, Object> info = new HashMap <Object, Object>();
info.put("info", obj);//json的根节点
//System.out.println(info);
/**
* 写
*/
String path="D:/test/json1.txt";
mapper.writeValue(new File(path), info);//把数据写入本地文件,JackSon会以json格式写入进去的
/**
* 读
*/
String jsons=info.toString().replace("=", ":");
//用Gson处理/初次使用JackSon找不到处理json数据,由于之前使用Gson就熟悉,就借用一下处理,感觉有点毛线。O(∩_∩)O哈哈~无语.
String newJson=new Gson().toJson(info);
int start=newJson.indexOf(":")+1;
int end=newJson.lastIndexOf("}");
jsons="["+newJson.substring(start, end)+"]";//组装JackSon支持的格式.
JsonFactory f = new JsonFactory();//实例JSON工程对象
JsonParser jp = f.createJsonParser(jsons);
jp.nextToken();//下一个JsonToken
while (jp.nextToken() == JsonToken.START_OBJECT) {
Info in