JsonArray转换为java代码

时间:2015-07-31 05:46:15

标签: java android json

{ 
"to": {
        "names": [
          "Mubshar Pribno"
        ],
        "callerIds": [
          "92 336 4440247"
        ],
        "captions": [
          "Mubshar Pribno"
        ]
      }
}

请建议我在java代码中创建这种类型的Json格式 请帮帮我

4 个答案:

答案 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

This is cool tutorial

答案 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)

你可以在java代码中创建一个json,因为你需要在java项目 json-simple-1.1.1.jar 中导入jar,你可以参考链接知道如何在java Click here to know how to create a json in java

中开发一个json