Note. I am EclipseLink JAXB (MOXy) and a member of JAXB (JSR-222) .
How should I structure my Java object?
. MOXy JSON JAXB JSON, . JAXB /, , .
MyResult
package forum11001458;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="MyResult")
public class MyResult {
@XmlElement(name="AccountID")
private String accountID;
@XmlElement(name="User")
private User user;
@XmlElement(name="Result")
private Result result;
}
package forum11001458;
import javax.xml.bind.annotation.XmlElement;
public class User {
@XmlElement(name="Name")
private String name;
@XmlElement(name="Email")
private String email;
}
package forum11001458;
import javax.xml.bind.annotation.XmlElement;
public class Result {
@XmlElement(name="Course")
private String course;
@XmlElement(name="Score")
private String score;
}
Json ?
, MOXy JSON:
jaxb.properties
MOXy JAXB, jaxb.properties , :
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Demo
, MOXy JSON - . API- Java SE 6. API, Java SE 5.
package forum11001458;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(MyResult.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty("eclipselink.media-type", "application/json");
File json = new File("src/forum11001458/input.json");
Object myResult = unmarshaller.unmarshal(json);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty("eclipselink.media-type", "application/json");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(myResult, System.out);
}
}
input.json/
{
"MyResult" : {
"AccountID" : "12345",
"User" : {
"Name" : "blah blah",
"Email" : "blah@blah.com"
},
"Result" : {
"Course" : "blah",
"Score" : "10.0"
}
}
}