<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 使用 Moxy 和 Jaxb 將 JSON 轉換為 Java 對象的示例 > 原文: [https://howtodoinjava.com/jaxb/convert-json-to-java-object-moxy/](https://howtodoinjava.com/jaxb/convert-json-to-java-object-moxy/) Java 示例**將 JSON 轉換為 Java 對象**。 您可以將 **JSON 字符串解組到對象**或將 **JSON 文件解組到對象**。 此示例將 [MOXy](https://wiki.eclipse.org/EclipseLink/Examples/MOXy) 與 JAXB 一起使用,以將 JSON 解組到 Java 對象。 MOXy 實現 JAXB,使開發人員可以通過注解提供其映射信息,并提供 JAXB 默認不提供的許多豐富功能。 ## 1\. MOXy 依賴 包括 MOXy 到項目運行時。 ```java <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.moxy</artifactId> <version>2.5.2</version> </dependency> ``` ## 2.將 JSON 文件轉換為 Java 對象 #### 2.1 添加 JAXB 注解 ```java @XmlRootElement(name = "employee") @XmlAccessorType(XmlAccessType.PROPERTY) public class Employee implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private String firstName; private String lastName; private Department department; public Employee() { super(); } //Setters and Getters } ``` ```java @XmlRootElement(name = "department") @XmlAccessorType(XmlAccessType.PROPERTY) public class Department implements Serializable { private static final long serialVersionUID = 1L; Integer id; String name; public Department() { super(); } //Setters and Getters } ``` #### 2.2 添加`jaxb.properties` 當您獲得`JAXBContext`的實例時,JAXB 將檢查`jaxb.properties`文件并構造上下文。 在這里,您從 MOXy 庫中注入了`JAXBContextFactory`。 > 將`jaxb.properties`文件放在放置 JAXB 注解類的同一包中。 ```java javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory ``` #### 3.將 JSON 文件轉換為對象 現在使用`javax.xml.bind.UnMarshaller`類將 json 轉換為對象。 ```java package com.howtodoinjava.demo; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import org.eclipse.persistence.jaxb.MarshallerProperties; import org.eclipse.persistence.jaxb.UnmarshallerProperties; import com.howtodoinjava.demo.model.Department; import com.howtodoinjava.demo.model.Employee; public class JaxbExample { public static void main(String[] args) { String fileName = "employee.json"; jaxbJsonToObject(fileName); } private static void jaxbJsonToObject(String fileName) { File xmlFile = new File(fileName); JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(Employee.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); //Set JSON type jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json"); jaxbUnmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true); Employee employee = (Employee) jaxbUnmarshaller.unmarshal(xmlFile); System.out.println(employee); } catch (JAXBException e) { e.printStackTrace(); } } } ``` 程序輸出: ```java Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]] ``` 讀取的 JSON 文件為: ```java { "employee" : { "department" : { "id" : 101, "name" : "IT" }, "firstName" : "Lokesh", "id" : 1, "lastName" : "Gupta" } } ``` ## 4.將 JSON 字符串轉換為 Java 對象 您可以獲取字符串形式的 JSON,然后直接填充到 Java 對象。 ```java package com.howtodoinjava.demo; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import org.eclipse.persistence.jaxb.MarshallerProperties; import org.eclipse.persistence.jaxb.UnmarshallerProperties; import com.howtodoinjava.demo.model.Department; import com.howtodoinjava.demo.model.Employee; public class JaxbExample { public static void main(String[] args) { String jsonString = "{\"employee\":{\"department\":{\"id\":101,\"name\":\"IT\"}, \"firstName\":\"Lokesh\",\"id\":1,\"lastName\":\"Gupta\"}}"; jaxbJsonStringToObject(jsonString); } private static void jaxbJsonStringToObject(String jsonString) { JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(Employee.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); //Set JSON type jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json"); jaxbUnmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true); Employee employee = (Employee) jaxbUnmarshaller.unmarshal(new StringReader(jsonString)); System.out.println(employee); } catch (JAXBException e) { e.printStackTrace(); } } } ``` 程序輸出: ```java Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]] ``` ## 5.將 Java 對象轉換為 JSON 將 Java 對象轉換為 JSON 的示例。 ```java package com.howtodoinjava.demo; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import org.eclipse.persistence.jaxb.MarshallerProperties; import com.howtodoinjava.demo.model.Department; import com.howtodoinjava.demo.model.Employee; public class JaxbExample { public static void main(String[] args) { Employee employee = new Employee(1, "Lokesh", "Gupta", new Department(101, "IT")); jaxbObjectToJSON(employee); } private static void jaxbObjectToJSON(Employee employee) { try { JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // To format JSON jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); //Set JSON type jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); jaxbMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); //Print JSON String to Console StringWriter sw = new StringWriter(); jaxbMarshaller.marshal(employee, sw); System.out.println(sw.toString()); } catch (JAXBException e) { e.printStackTrace(); } } } ``` 程序輸出: ```java { "employee" : { "department" : { "id" : 101, "name" : "IT" }, "firstName" : "Lokesh", "id" : 1, "lastName" : "Gupta" } } ``` > 閱讀更多:[將 Java 對象轉換為 XML](https://howtodoinjava.com/jaxb/write-object-to-xml/) 在評論部分中,向我發送您與編組和解組 json 有關的問題。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看