Json - Java对象到Json [英] Json - Java Object to Json

查看:109
本文介绍了Json - Java对象到Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Json的新手,我的目标是从Java bean创建下面的Json输出。我应该如何构建我的Java对象?我应该将MyResult类和User和Result作为子类吗?我可以使用哪些Json库?

I am very new to Json and my goal to create the Json output below from Java bean. How should I structure my Java object? Should I have MyResult class and User and Result as subclasses? What Json library can I use for this?

"MyResult" {
    "AccountID": "12345",
    "User" {
        "Name": "blah blah",
        "Email": "blah@blah.com",
     },
     "Result" {
         "Course": "blah",
         "Score": "10.0"
     }
 }


推荐答案

注意:我是 EclipseLink JAXB(MOXy) 领导和 JAXB(JSR-222) 专家组。

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.


我应该如何构建我的Java对象?

How should I structure my Java object?

下面是你的对象模型的样子。 MOXy的JSON绑定利用JAXB注释将域模型映射到JSON,所以我也包含了这些注释。 JAXB实现具有映射字段/属性名称的默认规则,但由于您的文档与默认值不同,因此必须对每个字段进行注释。

Below is what your object model could look like. MOXy's JSON binding leverages JAXB annotations for mapping the domain model to JSON, so I have included those as well. JAXB implementations have default rules for mapping field/property names, but since your document differs from the default each field had to be annotated.

MyResult

package forum11001458;

import javax.xml.bind.annotation.*;

@XmlRootElement(name="MyResult")
public class MyResult {

    @XmlElement(name="AccountID")
    private String accountID;

    @XmlElement(name="User")
    private User user;

    @XmlElement(name="Result")
    private Result result;

}

用户

package forum11001458;

import javax.xml.bind.annotation.XmlElement;

public class User {

    @XmlElement(name="Name")
    private String name;

    @XmlElement(name="Email")
    private String email;

}

结果

package forum11001458;

import javax.xml.bind.annotation.XmlElement;

public class Result {

    @XmlElement(name="Course")
    private String course;

    @XmlElement(name="Score")
    private String score;

}








我可以使用哪些Json库?

What Json library can I use for this?

以下是如何使用MOXy来执行JSON的方法绑定:

Below is how you can use MOXy to do the JSON binding:

jaxb.properties

要将MOXy用作JAXB提供程序,需要包含一个名为 jaxb.properties 的文件,并在与您的域模型相同的包中包含以下条目:

To use MOXy as your JAXB provider you need to include a file called jaxb.properties with the following entry in the same package as your domain model:

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

演示

注意MOXy的JSON绑定如何不需要任何编译时依赖性。 Java SE 6中提供了所有必需的API。如果您使用的是Java SE 5,则可以添加必要的支持API。

Note how MOXy's JSON binding does not require any compile time dependencies. All the necessary APIs are available in Java SE 6. You can add the necessary supporting APIs if you are using Java SE 5.

package forum11001458;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty("eclipselink.media-type", "application/json");
        File json = new File("src/forum11001458/input.json");
        Object myResult = unmarshaller.unmarshal(json);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty("eclipselink.media-type", "application/json");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(myResult, System.out);
    }

}

input.json /输出

{
   "MyResult" : {
      "AccountID" : "12345",
      "User" : {
         "Name" : "blah blah",
         "Email" : "blah@blah.com"
      },
      "Result" : {
         "Course" : "blah",
         "Score" : "10.0"
      }
   }
}

这篇关于Json - Java对象到Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆