Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to convert strings to list collections in json arrays

Shulou Source: shulou.com Published: 2022-05-31 10:38:49 10月06日 Update

本篇内容主要讲解"json数组怎么将字符串转list集合",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"json数组怎么将字符串转list集合"吧!

1.json字符串

json字符串与普通的字符串没有任何特殊的地方,但是之所以称为json字符串是因为,这个字符串符合我们之前介绍的语法规则。

2.json数组形式字符串转换为List的8种方法

package com.zkn.newlearn.json; import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject; import java.util.List;import java.util.Map; /** * Created by zkn on 2016/8/22. */public class JsonToMapTest02 { public static void main(String[] args){ String strArr = "[{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}," + "{\"00\":\"zhangsan\",\"11\":\"lisi\",\"22\":\"wangwu\",\"33\":\"maliu\"}]"; //第一种方式 List listObjectFir = (List) JSONArray.parse(strArr); System.out.println("利用JSONArray中的parse方法来解析json数组字符串"); for(Map mapList : listObjectFir){ for (Map.Entry entry : mapList.entrySet()){ System.out.println( entry.getKey() + " " +entry.getValue()); } } //第二种方式 List listObjectSec = JSONArray.parseObject(strArr,List.class); System.out.println("利用JSONArray中的parseObject方法并指定返回类型来解析json数组字符串"); for(Map mapList : listObjectSec){ for (Map.Entry entry : mapList.entrySet()){ System.out.println( entry.getKey() + " " +entry.getValue()); } } //第三种方式 JSONArray listObjectThir = JSONArray.parseArray(strArr); System.out.println("利用JSONArray中的parseArray方法来解析json数组字符串"); for(Object mapList : listObjectThir){ for (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue()); } } //第四种方式 List listObjectFour = JSONArray.parseArray(strArr,Map.class); System.out.println("利用JSONArray中的parseArray方法并指定返回类型来解析json数组字符串"); for(Object mapList : listObjectFour){ for (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue()); } } //第五种方式 JSONArray listObjectFifth = JSONObject.parseArray(strArr); System.out.println("利用JSONObject中的parseArray方法来解析json数组字符串"); for(Object mapList : listObjectFifth){ for (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue()); } } //第六种方式 List listObjectSix = JSONObject.parseArray(strArr,Map.class); System.out.println("利用JSONObject中的parseArray方法并指定返回类型来解析json数组字符串"); for(Object mapList : listObjectSix){ for (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue()); } } //第七种方式 JSONArray listObjectSeven = JSON.parseArray(strArr); System.out.println("利用JSON中的parseArray方法来解析json数组字符串"); for(Object mapList : listObjectSeven){ for (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue()); } } //第八种方式 List listObjectEigh = JSONObject.parseArray(strArr,Map.class); System.out.println("利用JSON中的parseArray方法并指定返回类型来解析json数组字符串"); for(Object mapList : listObjectEigh){ for (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue()); } } }}到此,相信大家对"json数组怎么将字符串转list集合"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Tags: Characters strings arrays methods methods types content learning practical common deeper special reason interest place practicality practice form simple operation more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia Shulou Information Linux Shulou Tech Info