使用Alibaba.FastJson将list转换成json
fastjson
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
项目地址:https://gitcode.com/gh_mirrors/fastj/fastjson
免费下载资源
·
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
public class ALiJson {
public static void main(String[] args) {
Ali ali1 = new ALiJson().new Ali();
ali1.setKey("aliKK1");
ali1.setValue("aliVV1");
Ali ali2 = new ALiJson().new Ali();
ali2.setKey("aliKK2");
ali2.setValue("aliVV2");
Ali ali3 = new ALiJson().new Ali();
ali3.setKey("aliKK3");
ali3.setValue("aliVV3");
List<Ali> list = new ArrayList<Ali>();
list.add(ali1);
list.add(ali2);
String jsonString = JSON.toJSONString(list);
System.out.println(jsonString);
}
public class Ali{
private String key;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
import java.util.List;
import com.alibaba.fastjson.JSON;
public class ALiJson {
public static void main(String[] args) {
Ali ali1 = new ALiJson().new Ali();
ali1.setKey("aliKK1");
ali1.setValue("aliVV1");
Ali ali2 = new ALiJson().new Ali();
ali2.setKey("aliKK2");
ali2.setValue("aliVV2");
Ali ali3 = new ALiJson().new Ali();
ali3.setKey("aliKK3");
ali3.setValue("aliVV3");
List<Ali> list = new ArrayList<Ali>();
list.add(ali1);
list.add(ali2);
list.add(ali3);
System.out.println(list);
JSONObject jsonObject = new JSONObject();
for(Ali ali : list) {
jsonObject.put(ali.getKey(), ali.getValue());
}
String jsonString2 = jsonObject.toJSONString();
System.out.println(jsonString2);
String jsonString = JSON.toJSONString(list);
System.out.println(jsonString);
}
public class Ali{
private String key;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
GitHub 加速计划 / fastj / fastjson
25.69 K
6.51 K
下载
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
最近提交(Master分支:2 个月前 )
c942c834 - 1 年前
5bc4709b - 1 年前
更多推荐
已为社区贡献2条内容
所有评论(0)