JSON字符串到对象的映射 [英] JSON String to Object Mapping

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

问题描述

我有一个JSON响应,我需要将对应的JSON字符串映射到特定的Response类,是否有任何工具或框架可以做到这一点.

I am having an JSON Response and what I need is to Map the corresponding JSON String to the particular Response class.Is there any tools or framework to do the same.

响应类为:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "0")
@XmlAccessorType(XmlAccessType.FIELD)
public class Student {

     @XmlElement(name="0")
     private String firstName;
     @XmlElement(name="1")
     private String lastName;

     public String getFirstName() {
         return firstName;
     }
     public void setFirstName(String firstName) {
         this.firstName = firstName;
     }
     public String getLastName() {
         return lastName;
     }
     public void setLastName(String lastName) {
         this.lastName = lastName;
     }
}

Json响应字符串为 {"0":{"0":"Rockey","1":"John"}}

Json Response String is {"0":{"0":"Rockey","1":"John"}}

我正在将Apache CXF Framework与Jettison一起使用,因为JSON Provider还使用JAXB将数据连接到低带宽客户端.

I am using Apache CXF Framework with Jettison as the JSON Provider also uses JAXB to wire the data to low bandwidth clients.

请注意,我要将数字表示形式转换为相应的字段.

Please make a note that I want to convert the number representations to corresponding fields.

推荐答案

注意:我是 JAXB(JSR-222) 专家组.

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

下面是如何用EclipseLink JAXB(MOXy)注释的Student类来支持用例.

Below is how you an support your use case with your Student class as annotated with EclipseLink JAXB (MOXy).

演示

import java.io.StringReader;
import java.util.*;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put("eclipselink.media-type", "application/json");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Student.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StringReader json = new StringReader("{\"0\":{\"0\":\"Rockey\",\"1\":\"John\"}}");
        Student student = (Student) unmarshaller.unmarshal(json);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(student, System.out);
    }

}

输出

{
   "0" : {
      "0" : "Rockey",
      "1" : "John"
   }
}

jaxb.properties

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

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

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


MOXy和JAX-RS

对于JAX-RS应用程序,您可以利用MOXyJsonProvider类来启用JSON绑定(请参阅:

For JAX-RS applications you can leverage the MOXyJsonProvider class to enable JSON-binding (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

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

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