3

我有一个 JSON 响应,我需要将相应的 JSON 字符串映射到特定的响应类。是否有任何工具或框架可以做同样的事情。

响应类是:

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

我正在使用带有 Jettison 的 Apache CXF 框架,因为 JSON 提供程序还使用 JAXB 将数据连接到低带宽客户端。

请记下我想将数字表示转换为相应的字段。

4

3 回答 3

2

您可以参考 Google-GSON 库 - https://github.com/google/gson

您还可以参考之前的 stackoverflow 答案 - Convert a JSON string to object in Java ME?

于 2012-10-30T07:38:30.353 回答
1

杰蒂森可以做到这一点。在此处找到使用 JAXB 将 JSON 解组为对象的示例代码:

JAXBContext jc = JAXBContext.newInstance(Customer.class);

JSONObject obj = new JSONObject("{\"customer\":{\"id\":123,\"first-name\":\"Jane\",\"last-name\":\"Doe\",\"address\":{\"street\":\"123 A Street\"},\"phone-number\":[{\"@type\":\"work\",\"$\":\"555-1111\"},{\"@type\":\"cell\",\"$\":\"555-2222\"}]}}");
Configuration config = new Configuration();
MappedNamespaceConvention con = new MappedNamespaceConvention(config);
XMLStreamReader xmlStreamReader = new MappedXMLStreamReader(obj, con);

Unmarshaller unmarshaller = jc.createUnmarshaller();
Customer customer = (Customer) unmarshaller.unmarshal(xmlStreamReader);
于 2012-10-30T07:42:31.923 回答
0

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

Student下面是您如何使用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在与您的域模型相同的包中调用的文件,其中包含以下条目:

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

MOXy 和 JAX-RS

对于 JAX-RS 应用程序,您可以利用MOXyJsonProvider该类来启用 JSON 绑定(请参阅:http ://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html )。

于 2012-10-30T10:12:23.250 回答