Java JSON对象展平

时间:2011-10-04 18:27:00

标签: java json

我正在寻找一个Java库来将我的域对象转换为扁平的JSON

例如

public class Person {
   String name
   Address homeAddress
}

public class Address {
  String street
  String zip
}

JSON: {name:'John', homeAddress_street: '123 Street', homeAddress_zip: 'xxxxx'}

我研究过XStream,Eclipse MOXy,FlexJSON,JSON-lib& GSON

我的目标是摆脱我的json包装器类并最小化代码。我希望有一个通用服务,可以采用我拥有的任何域模型类,并获得一个json表示,而无需为每种类型的模型编写xml描述符或任何自定义转换器。对于我的模型,深度为1级深度就足够了。我没有在上面的库中找到使用注释或内置功能的简单通用解决方案,但我可能忽略了它们。是否有非侵入式库可以做到这一点?或者也许是我列出的一个?我正在使用Hibernate,因此库必须能够处理CGLib代理

2 个答案:

答案 0 :(得分:2)

注意:我是EclipseLink JAXB (MOXy)主管,是JAXB (JSR-222)专家组的成员。

以下是利用@XmlPath扩展程序如何通过MOXy完成此操作的示例。

<强>人

package forum7652387;

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
    String name;

    @XmlPath(".")
    Address homeAddress;
}

<强>地址

package forum7652387;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Address {
    @XmlElement(name="homeAddress_street")
    String street;

    @XmlElement(name="homeAddress_zip")
    String zip;
}

<强> jaxb.properties

要将MOXy指定为JAXB提供程序,您需要在与域类相同的包中添加名为jaxb.properties的文件,并使用以下条目:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

<强>演示

package forum7652387;

import java.io.StringReader;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Person.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty("eclipselink.media-type", "application/json");
        unmarshaller.setProperty("eclipselink.json.include-root", false);
        String jsonString = "{\"name\":\"John\", \"homeAddress_street\":\"123 Street\", \"homeAddress_zip\":\"xxxxx\"}";
        StreamSource jsonSource = new StreamSource(new StringReader(jsonString));
        Person person = unmarshaller.unmarshal(jsonSource, Person.class).getValue();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty("eclipselink.media-type", "application/json");
        marshaller.setProperty("eclipselink.json.include-root", false);
        marshaller.marshal(person, System.out);
    }

}

<强>输出

 {"name" : "John", "homeAddress_street" : "123 Street", "homeAddress_zip" : "xxxxx"}

了解更多信息

答案 1 :(得分:-1)

我已经非常简单地使用了GSON,我认为它可以满足您的需求。在调查它时,我敲了下面的一个简单的例子来证实它就像我需要的一样简单;首先,POJO:

import java.util.List;

public class Member {

    private String name;
    private int age;
    private List<String> stuff;

    public Member() {
    }

    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 List<String> getStuff() {
        return stuff;
    }

    public void setStuff( List<String> stuff ) {
        this.stuff = stuff;
    }

}

然后,工人阶级:

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Main {

    public static void main( String[] args ) {
        Main m = new Main();
        m.execute( args );
    }

    private void execute( String[] args ) {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        Member member = new Member();

        List<String> stuff = new ArrayList<String>();
        stuff.add( "shoes" );
        stuff.add( "hat" );
        member.setStuff( stuff );
        member.setName( "Bert" );
        member.setAge( 21 );

        String output = gson.toJson( member, Member.class );
        log( output );

        Member member2 = gson.fromJson( output, Member.class );
        log(member2.getName());
    }

    private void log( String text ) {
        System.out.println( text );
    }

}

不需要xml描述符或自定义转换器。这是你要追求的那种东西吗?