package ivyy.taobao.com.domain.fackjson;
import ivyy.taobao.com.entity.About;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.ObjectMapper;
/**
*@Author:liangjl
*@Date:2015-1-5
*@Version:1.0
*@Description:jackjson ' :
* :jsonStr="[{\"age\":22,\"sex\":\" \",\"userName\":\"xiaoliang\"},{\"age\":22,\"sex\":\" \",\"userName\":\"xiaoliang\"}]";
* :jsonStr="[{'age':22,'sex':' ','userName':'xiaoliang'},{'age':22,'sex':' ','userName':'xiaoliang'}]";
*/
public class JackJson1 {
private static ObjectMapper mapper=new ObjectMapper();
public static void main(String[] args)throws Exception {
//json1.json
StringBuffer buff=new StringBuffer();
buff.append("[");
buff.append("{");
buff.append("\"weixin\"").append(":").append("\"YFDSBUYI\"").append(",");
buff.append("\"weibo\"").append(":").append("\"http://weibo.com/resourceljl\"").append(",");
buff.append("\"qq\"").append(":").append("\"1302128216\"").append(",");
buff.append("\"email\"").append(":").append("\"[email protected]\"").append(",");
buff.append("\"address\"").append(":").append("\" 39 \"").append("");
buff.append("}");
buff.append(",");//
buff.append("{");
buff.append("\"weixin\"").append(":").append("\"YFDSBUYI\"").append(",");
buff.append("\"weibo\"").append(":").append("\"http://weibo.com/resourceljl\"").append(",");
buff.append("\"qq\"").append(":").append("\"1302128216\"").append(",");
buff.append("\"email\"").append(":").append("\"[email protected]\"").append(",");
buff.append("\"address\"").append(":").append("\" 39 \"").append("");
buff.append("}");
buff.append("]");
String jsonStr=buff.toString();
System.out.println(jsonStr);
//jsonStr=mapper.writeValueAsString(jsonStr);
// JsonFactory
JsonFactory factory=new JsonFactory();
// JsonParser json
JsonParser parser=factory.createJsonParser(jsonStr);
// json
parser.nextToken();
while (parser.nextToken()==JsonToken.START_OBJECT) {
About a=mapper.readValue(parser, About.class);
System.out.println(a.getAddress());
}
}
}
package ivyy.taobao.com.entity;
import java.io.Serializable;
/**
*@Author:liangjilong
*@Date:2015-1-5
*@Version:1.0
*@Description:
*/
public class About implements Serializable {
private String weixin;
private String weibo;
private String email;
private String qq;
private String address;
public String getWeixin() {
return weixin;
}
public void setWeixin(String weixin) {
this.weixin = weixin;
}
public String getWeibo() {
return weibo;
}
public void setWeibo(String weibo) {
this.weibo = weibo;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}