当前位置: 代码迷 >> JavaScript >> 施用XStream把Java对象XML/JSON格式的序列化和反序列化
  详细解决方案

施用XStream把Java对象XML/JSON格式的序列化和反序列化

热度:678   发布时间:2012-11-10 10:48:50.0
使用XStream把Java对象XML/JSON格式的序列化和反序列化
试了一下Thoughtworks的XStream,以测试驱动和敏捷开发著称的Thoughtworks,
写出的XStream果然很容易使用。
下面举一个用一个小例子说明如何使用:
实体Person,我们希望Person的对象能够和XML/JSON相互转化
package edu.jlu.fuliang;

public class Person
{
    private String name;
    private int age;
    private Address address;
    private String email;

    
    public Person()
    {
    }

    public Person( String name, int age, Address address, String email )
    {
        this.name = name;
        this.age = age;
        this.address = address;
        this.email = email;
    }

    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 Address getAddress()
    {
        return address;
    }

    public void setAddress( Address address )
    {
        this.address = address;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail( String email )
    {
        this.email = email;
    }

    @Override
    public String toString()
    {
        return "name:" + name + ",age:" + age + ",address:" + address + ",email:" + email;
    }
}


package edu.jlu.fuliang;

public class Address
{
    private String street;
    private String city;
    
    public Address()
    {
    }

    
    public Address( String street, String city )
    {
        this.street = street;
        this.city = city;
    }


    public String getStreet()
    {
        return street;
    }

    public void setStreet( String street )
    {
        this.street = street;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity( String city )
    {
        this.city = city;
    }
    
    @Override
    public String toString()
    {
        return "street:" + street + ",city:" + city;
    }
}

写一个测试来看看如何使用XStream:
package edu.jlu.fuliang;

import junit.framework.TestCase;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

public class XStreamTest extends TestCase
{
    private Person person;
    @Override
    protected void setUp() throws Exception
    {
        Address address = new Address("中关村","北京");
        person = new Person("张三",12,address,"zhangsan@gmail.com");
    }
 
    public void testObjectXML()
    {
        XStream xStream = new XStream();
        xStream.alias( "Person", Person.class);
        xStream.alias( "Address", Address.class );
        String xml = xStream.toXML( person );
        System.out.println("Serialize person to XML:\n" + xml);
        
        Person p = (Person) xStream.fromXML( xml );
        System.out.println("Deserialize XML to person:\n" + p);
    }
    
    public void testObjectJSON(){
        XStream xStream = new XStream(new JettisonMappedXmlDriver());
        xStream.setMode( XStream.NO_REFERENCES );
        xStream.alias( "Person", Person.class );
        xStream.alias("Address", Address.class);
        String json = xStream.toXML( person );
        System.out.println("Serialize person to JSON:\n" + json);
        
        Person p = (Person) xStream.fromXML( json );
        System.out.println("Deserialize JSON to person:\n" + p);
    }
}

结果:
引用

<Person>
  <name>张三</name>
  <age>12</age>
  <address>
    <street>中关村</street>
    <city>北京</city>
  </address>
  <email>zhangsan@gmail.com</email>
</Person>

Serialize person to XML:
<Person>
  <name>张三</name>
  <age>12</age>
  <address>
    <street>中关村</street>
    <city>北京</city>
  </address>
  <email>zhangsan@gmail.com</email>
</Person>

Deserialize XML to person:
name:张三,age:12,address:street:中关村,city:北京,email:zhangsan@gmail.com

Serialize person to JSON:
{"Person":{"name":"张三","age":12,"address":{"street":"中关村","city":"北京"},"email":"zhangsan@gmail.com"}}

Deserialize json to person:
name:张三,age:12,address:street:中关村,city:北京,email:zhangsan@gmail.com


1 楼 yajie 2009-05-22  
太麻烦了。
2 楼 fuliang 2009-05-22  
yajie 写道

太麻烦了。

晕,这还麻烦? 只用了这么一句话
String xml = xStream.toXML( person ); 
请教一个更简洁的lib
3 楼 LewJun 2012-04-26  
这里面有将json转换成为对象的方法。
下面的json是使用了List之后生成的。

请问:
应该怎么将这段json   {'teachers': [ {'id': 1, 'name': 'Rose'}, { 'id': 3, 'name': 'Tony' } ]}
还原成为对象?
4 楼 fuliang 2012-05-13  
testObjectJSON里面已经有演示了

LewJun 写道
这里面有将json转换成为对象的方法。
下面的json是使用了List之后生成的。

请问:
应该怎么将这段json   {'teachers': [ {'id': 1, 'name': 'Rose'}, { 'id': 3, 'name': 'Tony' } ]}
还原成为对象?

  相关解决方案