Python 官方文档:入门教程 => 点击学习
目录1:JSONArray转List2:List转jsONArray1:JSONArray转List JSONArray字符串 转 List //初始化JSONArray JSONA
JSONArray字符串 转 List
//初始化JSONArray
JSONArray array=new JSONArray();
array.add(0,"a");
array.add(1,"b");
array.add(2,"c");
List<String> list = JSONObject.parseArray(array.toJSONString(),String.class);
System.out.println(list.toString());
输出结果:
[a, b, c]
JSONArray对象 转 List
//初始化JSONArray
JSONArray jsonArray = JSONArray.parseArray("[{\"name\":\"zhangsan\",\"age\":18},{\"name\":\"lisi\",\"age\":23}]");
//打印jsonArray
System.out.println(jsonArray.toJSONString());
//转List
List<Body> list = JSONArray.parseArray(jsonArray.toJSONString(), Body.class);
//打印List<Body>
System.out.println(list.toString());
Body.class代码:
static class Body implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Body{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
输出结果:
[{"name":"zhangsan","age":18},{"name":"lisi","age":23}]
[Body{name='zhangsan', age=18}, Body{name='lisi', age=23}]
List<T> list = new ArrayList<T>();
JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list));
到此这篇关于java中JSONArray互相转换List的实现的文章就介绍到这了,更多相关java JSONArray互相转换List内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: java中JSONArray互相转换List的实现
本文链接: https://lsjlt.com/news/153997.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0