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

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

问题描述

我对 Json 非常陌生,我的目标是从 Java bean 创建下面的 Json 输出.我应该如何构建我的 Java 对象?我应该将 MyResult 类和 User 和 Result 作为子类吗?我可以为此使用什么 Json 库?

我的结果"{帐户ID":12345",用户"{"name": "blah blah","电子邮件": "blah@blah.com",},结果" {课程":等等",分数":10.0"}}

解决方案

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


<块引用>

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

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

我的结果

包forum11001458;导入 javax.xml.bind.annotation.*;@XmlRootElement(name="MyResult")公共类 MyResult {@XmlElement(name="AccountID")私人字符串帐户ID;@XmlElement(name="用户")私人用户用户;@XmlElement(name="结果")私人结果结果;}

用户

包forum11001458;导入 javax.xml.bind.annotation.XmlElement;公共类用户{@XmlElement(name="姓名")私人字符串名称;@XmlElement(name="电子邮件")私人字符串电子邮件;}

结果

包forum11001458;导入 javax.xml.bind.annotation.XmlElement;公共课结果{@XmlElement(name="课程")私人弦乐课程;@XmlElement(name="分数")私人字符串分数;}


<块引用>

我可以为此使用什么 Json 库?

以下是如何使用 MOXy 进行 JSON 绑定:

jaxb.properties

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

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

演示

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

包forum11001458;导入 java.io.File;导入 javax.xml.bind.*;公开课演示{public static void main(String[] args) 抛出异常 {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");对象 myResult = unmarshaller.unmarshal(json);Marshaller marshaller = jc.createMarshaller();marshaller.setProperty("eclipselink.media-type", "application/json");marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,真);marshaller.marshal(myResult, System.out);}}

input.json/输出

<代码>{我的结果":{"帐户ID" : "12345",用户":{"名称": "等等",电子邮件":blah@blah.com"},结果" : {课程":等等",分数":10.0"}}}

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"
     }
 }

解决方案

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


How should I structure my Java object?

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;

}

User

package forum11001458;

import javax.xml.bind.annotation.XmlElement;

public class User {

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

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

}

Result

package forum11001458;

import javax.xml.bind.annotation.XmlElement;

public class Result {

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

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

}


What Json library can I use for this?

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

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

Demo

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/Output

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

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

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