Java对象转换成xml对象和Java对象转换成JSON对象

 1.把Java对象转换成JSON对象

apache提供的json-lib小工具,它可以方便的使用Java语言来创建JSON字符串。也可以把JavaBean转换成JSON字符串。

json-lib的核心jar包有: 

json-lib.jar,只有两个核心类: JSONObject, JSONArray;

json-lib的依赖jar包有:

commons-lang.jar

commons-beanutils.jar

commons-logging.jar

commons-collections.jar

ezmorph.jar

将person对象转化成json对象:

javabean:

package java转json;

public class Person {
private String name;
private int age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
} }

用junit测试:

    @Test
public void fun() {
JSONObject jo = new JSONObject();
jo.put("name", "张三");
jo.put("age", 12);
jo.put("sex", "男");
String s = jo.toString();
System.out.println(s);
} @Test
public void fun1() {
Person p = new Person("liSi", 32, "female");
JSONObject jo = JSONObject.fromObject(p);// 把JavaBean对象转换成json
System.out.println(jo.toString());
}
// 结果为:{"age":32,"name":"liSi","sex":"female"} @Test
public void fun2() {
Person p = new Person("liSi", 32, "female");
Person p1 = new Person("zhangSan", 312, "female");
Person[] pe = { p, p1 };
JSONArray ja = JSONArray.fromObject(pe);// 把数组转换成JSONArray
System.out.println(ja.toString()); }
// 结果:[{"age":312,"name":"zhangSan","sex":"female"},{"age":32,"name":"liSi","sex":"female"}] @Test
public void fun3() {
Person p = new Person("liSi", 32, "female");
Person p1 = new Person("zhangSan", 312, "female");
List<Person> l = new ArrayList<Person>();
l.add(p1);
l.add(p);
System.out.println(JSONArray.fromObject(l).toString());// 把list转换成JSONArray
}
// 结果为:[{"age":312,"name":"zhangSan","sex":"female"},{"age":32,"name":"liSi","sex":"female"}]

2.把Java对象转换成xml对象

可以到http://xstream.codehaus.org/地址去下载XStream安装包!

XStream的必导JAR包:

核心JAR包:xstream-1.4.7.jar;

必须依赖包:xpp3_min-1.1.4c(XML Pull Parser,一款速度很快的XML解析器);

javabean对象仍然用person;

public List<Person> p() {
Person p1 = new Person("tian", 12, "男");
Person p2 = new Person("di", 14, "nv");
Person p3 = new Person("wo", 23, "a");
List<Person> l = new ArrayList<Person>();
l.add(p1);
l.add(p2);
l.add(p3);
return l;
} @Test
public void fun4() {
List<Person> l = p();
XStream xs = new XStream();// 创建XStream对象 调用toXML把集合转换成xml字符串
String s = xs.toXML(l);
System.out.println(s);
}
/*结果
<list>
<java转xml.Person>
<name>tian</name>
<age>12</age>
<sex>男</sex>
</java转xml.Person>
<java转xml.Person>
<name>di</name>
<age>14</age>
<sex>nv</sex>
</java转xml.Person>
<java转xml.Person>
<name>wo</name>
<age>23</age>
<sex>a</sex>
</java转xml.Person>
</list>
*/
@Test
public void fun2() {
List<Person> l = p();
XStream xs = new XStream();
xs.alias("value", List.class);// 给List起别名为china
xs.alias("person", Person.class);// 给person起别名为person
xs.useAttributeFor(Person.class, "name");// 把Person类型的name属性,生成<person>元素的属性
xs.omitField(Person.class, "age");// 让City类的,名为age属性不生成对应的xml元素
String s = xs.toXML(l);
System.out.println(s);
}
/*
* <value>
<person name="tian">
<sex>男</sex>
</person>
<person name="di">
<sex>nv</sex>
</person>
<person name="wo">
<sex>a</sex>
</person>
</value> */
上一篇:Phpcms·二次开发


下一篇:Java循环中删除一个列表元素