json字符串转换为Array or List
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
- 将json字符串转成Java的Array数组
- private String json = "{\"address\":\"chian\",\"birthday\":{\"birthday\":\"2010-11-22\"},"+
- "\"email\":\"email@123.com\",\"id\":22,\"name\":\"tom\"}";
- @Test
- public void readJSON2Array() {
- try {
- json = "[" + json + "]";
- jsonArray = JSONArray.fromObject(json);
- Object[] os = jsonArray.toArray();
- System.out.println(os.length);
- Student[] stus = (Student[]) JSONArray.toArray(jsonArray, Student.class);
- System.out.println(stus.length);
- System.out.println(stus[0]);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- 运行的结果如下:
- ==============JSON Arry String >>> Java Array ==================
- #%%%{"address":"chian","birthday":{"birthday":"2010-11-22"},"email":"email@123.com","id":22,"name":"tom"}
- 1
- {"address":"chian","birthday":{"birthday":"2010-11-22"},"email":"email@123.com","id":22,"name":"tom"}
- {"address":"chian","birthday":{"birthday":"2010-11-22"},"email":"email@123.com","id":22,"name":"tom"}
- 1
- 将JSON字符串转成Java的List集合
- private String json = "{\"address\":\"chian\",\"birthday\":{\"birthday\":\"2010-11-22\"},"+
- "\"email\":\"email@123.com\",\"id\":22,\"name\":\"tom\"}";
- public void readJSON2List() {
- try {
- json = "[" + json + "]";
- jsonArray = JSONArray.fromObject(json);
- List<Student> list = JSONArray.toList(jsonArray, Student.class);
- System.out.println(list.size());
- System.out.println(list.get(0));
- list = JSONArray.toList(jsonArray);
- System.out.println(list.size());
- System.out.println(list.get(0));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- 运行后结果如下:
- ==============JSON Arry String >>> Java List ==================
- 1
- tom#22#chian#2010-11-22#email@123.com
- 1
- net.sf.ezmorph.bean.MorphDynaBean@141b571[
- {id=22, birthday=net.sf.ezmorph.bean.MorphDynaBean@b23210[
- {birthday=2010-11-22}
- ], address=chian, email=email@123.com, name=tom}
- ]
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e
3 个月前
8c391e04
6 个月前
更多推荐
已为社区贡献2条内容
所有评论(0)