Frage

Ich möchte JAXB in einer Javase -Standalone -Anwendung zum Lesen und Schreiben von JSON -Dateien verwenden. Ich habe es geschafft, es für XML -Dateien mit meinem Snippet unten zu tun, aber ich verstehe nicht, wie man zu JSON wechselt:

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

}

Anmerkungen:

  • Leistung ist kein Problem.
  • Webdienste / Ruhe ist nicht mehr zu tun.
War es hilfreich?

Lösung

Mit Eclipselink Moxy können Sie Eigenschaften hinzufügen, um die Ausgabe zu manipulieren.

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

}

Wird in: {"Buch": {"title": "hello"}} führen

Sowohl org.eclipse.Persistence.Moxy-2.5.1.jar und org.eclipse.persistence.core-2.5.1.jar sind auf dem Klassenpfad erforderlich. Während ich selbst damit herumspielte, stieß ich auf: Heißeste JAXB Antworten. Besonders blaise Doughan antwortet, wo sehr hilfreich ist. Suchen nach

MarshallerProperties.MEDIA_TYPE, "application/json" 

für mehr seiner Beispiele.

Andere Tipps

Wenn Sie möchten, können Sie eine separate Methode wie diese sehr einfach erstellen (wenn Sie bereits die JAXB -Libs haben)

die Hauptklasse

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

Hier ist das 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;
    }

}

}

Sie würden es so machen:

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);
 }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top