簡體   English   中英

JSON字符串到對象的映射

[英]JSON String to Object Mapping

我有一個JSON響應,我需要將對應的JSON字符串映射到特定的Response類,是否有任何工具或框架可以做到這一點。

響應類為:

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 Framework一起使用,因為JSON Provider還使用JAXB將數據連接到低帶寬客戶端。

請注意,我要將數字表示形式轉換為相應的字段。

您可以參考Google-GSON庫-https: //github.com/google/gson

您還可以參考早期的stackoverflow答案- 將JSON字符串轉換為Java ME中的對象?

傑蒂森可以做到這一點。 在此處找到了將JSON編組為JAXB對象的示例代碼:

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);

注意:我是EclipseLink JAXB(MOXy)的負責人,並且是JAXB(JSR-222)專家組的成員。

以下是用EclipseLink JAXB(MOXy)注釋的Student類使用案例的支持方式。

演示版

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 : MOXyJsonProvider )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM