java JSON 字符串到对象映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13134563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON String to Object Mapping
提问by Prabhath kesav
I am having an JSON Response and what I need is to Map the corresponding JSON String to the particular Response class.Is there any tools or framework to do the same.
我有一个 JSON 响应,我需要的是将相应的 JSON 字符串映射到特定的响应类。是否有任何工具或框架可以做同样的事情。
Response class is:
响应类是:
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 Response String is {"0":{"0":"Rockey","1":"John"}}
Json 响应字符串为 {"0":{"0":"Rockey","1":"John"}}
I am using Apache CXF Framework with Jettison as the JSON Provider also uses JAXB to wire the data to low bandwidth clients.
我将 Apache CXF 框架与 Jettison 一起使用,因为 JSON 提供程序还使用 JAXB 将数据连接到低带宽客户端。
Please make a note that I want to convert the number representations to corresponding fields.
请注意,我想将数字表示转换为相应的字段。
采纳答案by bdoughan
Note:I'm the EclipseLink JAXB (MOXy)lead and a member of the JAXB (JSR-222)expert group.
注意:我是EclipseLink JAXB (MOXy) 的负责人,也是JAXB (JSR-222)专家组的成员。
Below is how you an support your use case with your Student
class as annotated with EclipseLink JAXB (MOXy).
下面是您如何使用Student
EclipseLink JAXB (MOXy) 注释的类来支持您的用例。
Demo
演示
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);
}
}
Output
输出
{
"0" : {
"0" : "Rockey",
"1" : "John"
}
}
jaxb.properties
jaxb.properties
To use MOXy as your JAXB provider you need to include a file called jaxb.properties
in the same package as your domain model with the following entry:
要将 MOXy 用作您的 JAXB 提供程序,您需要包含一个jaxb.properties
在与域模型相同的包中调用的文件,其中包含以下条目:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
MOXy and JAX-RS
MOXy 和 JAX-RS
For JAX-RS applications you can leverage the MOXyJsonProvider
class to enable JSON-binding (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).
对于 JAX-RS 应用程序,您可以利用MOXyJsonProvider
该类来启用 JSON 绑定(请参阅:http: //blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)。
回答by Yashpal Singla
You can refer Google-GSON library - https://github.com/google/gson
您可以参考 Google-GSON 库 - https://github.com/google/gson
You can also refer earlier stackoverflow answer - Convert a JSON string to object in Java ME?
您还可以参考较早的 stackoverflow 答案 -在 Java ME 中将 JSON 字符串转换为对象?
回答by artplastika
Jettison could do this. Found a sample code of unmarshalling JSON to object with JAXB here:
杰蒂森可以做到这一点。在此处找到了使用 JAXB 将 JSON 解组为对象的示例代码:
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);