本代码用的是fastjson,Y用这个包 java.util.List
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jiekou</groupId>
<artifactId>jiekou</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.27</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.8</version>
</dependency>
</dependencies>
</project>
import java.util.List;
/**
* Created by Administrator on 2017/8/7 0007.
*/
public class MyUser {
private String userName;
private int age;
private List list;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Created by Administrator on 2017/8/7 0007.
*/
public class Aa {
private MyUser user;
private Map<String,Object> map;
private List<MyUser> myUserList;
public MyUser getUser() {
return user;
}
public void setUser(MyUser user) {
this.user = user;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public List<MyUser> getMyUserList() {
return myUserList;
}
public void setMyUserList(List<MyUser> myUserList) {
this.myUserList = myUserList;
}
}
import java.util.Map;
/**
* Created by Administrator on 2017/8/8 0008.
*/
public class No1 {
private String name;
private No2 no2;
private Map map;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public No2 getNo2() {
return no2;
}
public void setNo2(No2 no2) {
this.no2 = no2;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
}
import java.util.List;
/**
* Created by Administrator on 2017/8/8 0008.
*/
public class No2 {
private String name;
private MyUser myUser;
private List list;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public MyUser getMyUser() {
return myUser;
}
public void setMyUser(MyUser myUser) {
this.myUser = myUser;
}
}
package com.testjson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.longteng.test.MyUser;
import com.longteng.test.No1;
import com.longteng.test.No2;
import org.junit.Test;
import sun.applet.Main;
import javax.activation.MailcapCommandMap;
import java.rmi.MarshalledObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2017/8/7 0007.
*/
public class JsonTest {
public static void main(String[] args){
MyUser myUser = new MyUser();
myUser.setUserName("niu");
myUser.setAge(28);
//序列化 把对象转换成json字符串
String myJson =JSON.toJSONString(myUser);
System.out.print(myJson);
//反序列化,把JSON字符串转换成对象
MyUser myUser1 = JSON.parseObject(myJson, MyUser.class);
System.out.print(myUser1);
//map
Map map = new HashMap();
map.put("username","niujin");
map.put("age","28");
//序列化
String map1 = JSON.toJSONString(map);
System.out.print(map1);
// 反序列化
MyUser myUser2 = JSON.parseObject(map1,Map.class);
System.out.print(myUser2);
//list
List list = new ArrayList();
list.add("list");
list.add("333");
//序列化
String list1 = JSON.toJSONString(list);
System.out.print(list1);
//反序列化
List list2 = (List)JSON.parseArray(list1);
System.out.print(list1);
}
@Test //数组
public void jsonArray(){
String[] strings = new String[2];
strings[0] = "niu";
strings[1] = "123";
//序列化
String myString = JSON.toJSONString(strings);
System.out.print(myString);
//反序列化
JSONArray strings1 = JSON.parseArray(myString);
System.out.print(strings1);
}
@Test
public void TestArrayObject(){
No1 no1 = new No1();
no1.setName("niu");
Map map = new HashMap();
map.put("niu","jinliang");
no1.setMap(map);
No2 no2 = new No2();
no2.setName("ceshi");
List<String> list = new ArrayList<String>();
list.add("第几层");
no2.setList(list);
MyUser myUser = new MyUser();
myUser.setUserName("我是myUser 里的username");
List<String> list1 = new ArrayList<String>();
list1.add("我是myUser里的list");
myUser.setList(list1);
no2.setMyUser(myUser);
no1.setNo2(no2);
//序列化
String json = JSON.toJSONString(no1, SerializerFeature.WriteClassName);
System.out.print(json);
//反序列化
No1 no11 = (No1)JSON.parse(json);
System.out.print(no11);
}
}
package com.testxml;
import com.longteng.test.MyUser;
import com.thoughtworks.xstream.XStream;
import org.junit.Test;
/**
* Created by Administrator on 2017/8/8 0008.
*/
public class XmlJavaBeanTest {
@Test
public void objToJson(){
XStream xStream = new XStream();
MyUser myUser = new MyUser();
myUser.setUserName("niu");
myUser.setAge(28);
String xmlObj = xStream.toXML(myUser);
System.out.print(xmlObj);
MyUser myUser1 = (MyUser)xStream.fromXML(xmlObj);
System.out.print(myUser1);
}
}