我想在 JavaSE 独立应用程序中使用 JAXB 来读取和写入 Json 文件。我设法使用下面的代码片段对 XML 文件执行此操作,但我不明白如何切换到 Json:

public class Main {

  public static void main(String[] args) throws Exception {
    Book book = new Book();
    book.title = "hello";

    JAXBContext context = JAXBContext.newInstance(Book.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.marshal(book, System.out);
  }

}

笔记:

  • 性能不是问题。
  • Web 服务/REST 已超出主题。
有帮助吗?

解决方案

使用Eclipselink Moxy,您可以添加属性来操纵输出。

    public class Main {

    public static void main(String[] args) {
        Book book = new Book();
        book.title = "hello";

        JAXBContext context;
        try {
            context = JAXBContextFactory.createContext(new Class[] {Book.class}, null);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(book, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

}

将导致:{“ book”:{“ title”:“ hello”}}

org.eclipse.persistence.moxy-2.5.1.jar and org.eclipse.persistence.core-core-2.5.1.jar在classPath上需要jar。当我自己玩这个时,我遇到了: 最热门的JAXB答案. 。尤其是Blaise Doughan回答非常有帮助的地方。搜索

MarshallerProperties.MEDIA_TYPE, "application/json" 

有关他的更多例子。

其他提示

如果愿意,您可以非常简单地创建一个单独的方法(如果您已经拥有JAXB Libs)

主班

import java.io.IOException;
import javax.xml.bind.JAXBException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class ConvertJson {

    public static void main(String[] args) {
        final JavaObject javaObject = new JavaObject();
        javaObject.setName("Json");
        javaObject.setRole("Moderator");
        javaObject.setAge(28);

        try {
            pojo2Json(javaObject);
        } catch (JAXBException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static String pojo2Json(Object obj) throws JAXBException,
            JsonParseException, JsonMappingException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(obj);
        System.out.print(jsonString);
        return jsonString;
    }

这是Pojo

import org.codehaus.jackson.annotate.JsonProperty;
public class JavaObject{
    @JsonProperty(value = "Name")
    private String name;
    @JsonProperty(value = "Role")
    private String role;
    @JsonProperty(value = "Age")
    private int age;
    public JavaObject(){
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}

}

你会这样做:

public class Main {
 public static void main(String[] args) throws IOException {
  Book book = new Book();
  book.title = "hello";

  ObjectMapper mapper = new ObjectMapper();
  mapper.writeValue(System.out, book);
 }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top