{
"to": {
"names": [
"Mubshar Pribno"
],
"callerIds": [
"92 336 4440247"
],
"captions": [
"Mubshar Pribno"
]
}
}
请建议我在java代码中创建这种类型的Json
格式
请帮帮我
答案 0 :(得分:0)
GSON是Google的一个Java库,用于将Java对象转换为JSON。您只需将Java对象传递给库函数,它就会返回一个JSON字符串。
下载:http://code.google.com/p/google-gson/
示例:http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
答案 1 :(得分:0)
我建议您使用Jackson。
答案 2 :(得分:0)
对于上述json,您必须拥有以下课程:
<强> Example.java 强>
package com.example;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
public class Example {
@Expose
private To to;
public To getTo() {
return to;
}
public void setTo(To to) {
this.to = to;
}
}
<强> To.java 强>
package com.example;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
public class To {
@Expose
private List<String> names = new ArrayList<String>();
@Expose
private List<String> callerIds = new ArrayList<String>();
@Expose
private List<String> captions = new ArrayList<String>();
public List<String> getNames() {
return names;
}
public void setNames(List<String> names) {
this.names = names;
}
public List<String> getCallerIds() {
return callerIds;
}
public void setCallerIds(List<String> callerIds) {
this.callerIds = callerIds;
}
public List<String> getCaptions() {
return captions;
}
public void setCaptions(List<String> captions) {
this.captions = captions;
}
}
然后将此类转换为json字符串,如下所示:
Gson gson = new Gson();
To myTo=new To();
myTo.setNames(...);
myTo.setCallerIds(...);
myTO.setCaptions(...);
Example exm=new Example();
exm.setTo(myTo);
System.out.println(gson.toJson(exm));
答案 3 :(得分:0)