map转json数组(map转json字符串)

map转json数组(map转json字符串)Json 数组形式的字符串转为 Map 数组 String strArr name xx age 16 name yy age amp ldquo

大家好,我是讯享网,很高兴认识大家。



// Json数组形式的字符串转为Map数组  String strArr = “[{“name”: “xx”, “age”: “16”},{“name”: “yy”, “age”: “18”}]”;

讯享网

讯享网</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第一种方式</span> List&lt;Map&lt;String,String&gt;&gt; listObjectFir = (List&lt;Map&lt;String,String&gt;&gt;<span style="color: rgba(0, 0, 0, 1)">) JSONArray.parse(strArr); System.out.println(</span>"利用JSONArray中的parse方法来解析json数组字符串"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span>(Map&lt;String,String&gt;<span style="color: rgba(0, 0, 0, 1)"> mapList : listObjectFir){ </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Map.Entry entry : mapList.entrySet()){ System.out.println( entry.getKey() </span>+ " " +<span style="color: rgba(0, 0, 0, 1)">entry.getValue()); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第二种方式</span> List&lt;Map&lt;String,String&gt;&gt; listObjectSec = JSONArray.parseObject(strArr,List.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"利用JSONArray中的parseObject方法并指定返回类型来解析json数组字符串"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span>(Map&lt;String,String&gt;<span style="color: rgba(0, 0, 0, 1)"> mapList : listObjectSec){ </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Map.Entry entry : mapList.entrySet()){ System.out.println( entry.getKey() </span>+ " " +<span style="color: rgba(0, 0, 0, 1)">entry.getValue()); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第三种方式</span> JSONArray listObjectThir =<span style="color: rgba(0, 0, 0, 1)"> JSONArray.parseArray(strArr); System.out.println(</span>"利用JSONArray中的parseArray方法来解析json数组字符串"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(Object mapList : listObjectThir){ </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() </span>+ " " +<span style="color: rgba(0, 0, 0, 1)">((Map.Entry)entry).getValue()); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第四种方式</span> List listObjectFour = JSONArray.parseArray(strArr,Map.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"利用JSONArray中的parseArray方法并指定返回类型来解析json数组字符串"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(Object mapList : listObjectFour){ </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() </span>+ " " +<span style="color: rgba(0, 0, 0, 1)">((Map.Entry)entry).getValue()); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第五种方式</span> JSONArray listObjectFifth =<span style="color: rgba(0, 0, 0, 1)"> JSONObject.parseArray(strArr); System.out.println(</span>"利用JSONObject中的parseArray方法来解析json数组字符串"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(Object mapList : listObjectFifth){ </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() </span>+ " " +<span style="color: rgba(0, 0, 0, 1)">((Map.Entry)entry).getValue()); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第六种方式</span> List listObjectSix = JSONObject.parseArray(strArr,Map.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"利用JSONObject中的parseArray方法并指定返回类型来解析json数组字符串"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(Object mapList : listObjectSix){ </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() </span>+ " " +<span style="color: rgba(0, 0, 0, 1)">((Map.Entry)entry).getValue()); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第七种方式</span> JSONArray listObjectSeven =<span style="color: rgba(0, 0, 0, 1)"> JSON.parseArray(strArr); System.out.println(</span>"利用JSON中的parseArray方法来解析json数组字符串"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(Object mapList : listObjectSeven){ </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() </span>+ " " +<span style="color: rgba(0, 0, 0, 1)">((Map.Entry)entry).getValue()); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第八种方式</span> List listObjectEigh = JSONObject.parseArray(strArr,Map.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"利用JSON中的parseArray方法并指定返回类型来解析json数组字符串"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(Object mapList : listObjectEigh){ </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Object entry : ((Map)mapList).entrySet()){ System.out.println(((Map.Entry)entry).getKey() </span>+ " " +<span style="color: rgba(0, 0, 0, 1)">((Map.Entry)entry).getValue()); } } 

// map集合遍历 Map&lt;String, Integer&gt; tempMap = new HashMap&lt;String, Integer&gt;(); tempMap.put(“a”,“12”); tempMap.put(“b”,“34”); tempMap.put(“c”,“56”);

// 遍历方法一 hashmap entrySet() 遍历 Iterator it = tempMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); Object key = entry.getKey(); Object value = entry.getValue(); System.out.println(“key=” + key + “ value=” + value); }


讯享网

// 遍历方法二 for (Map.Entry&lt;String, Integer&gt; entry : tempMap.entrySet()) { String key = entry.getKey().toString(); String value = entry.getValue().toString(); System.out.println(“key=” + key + “ value=” + value); }

// 遍历方法三 hashmap keySet() 遍历 for (Iterator i = tempMap.keySet().iterator(); i.hasNext();) { Object obj = i.next(); System.out.println(obj);// 循环输出key System.out.println(“key=” + obj + “ value=” + tempMap.get(obj)); } for (Iterator i = tempMap.values().iterator(); i.hasNext();) { Object obj = i.next(); System.out.println(obj);// 循环输出value }

// 遍历方法四 treemap keySet()遍历 for (Object o : tempMap.keySet()) { System.out.println(“key=” + o + “ value=” + tempMap.get(o)); }

// java如何遍历Map &lt;String, ArrayList&gt; map = new HashMap &lt;String, // ArrayList&gt;(); System.out.println(“java 遍历Map &lt;String, ArrayList&gt; map = new HashMap&lt;String, ArrayList&gt;();”); Map&lt;String, ArrayList&gt; map = new HashMap&lt;String, ArrayList&gt;(); Set&lt;String&gt; keys = map.keySet(); Iterator&lt;String&gt; iterator = keys.iterator(); while (iterator.hasNext()) { String key = iterator.next(); ArrayList arrayList = map.get(key); for (Object o : arrayList) {

System.out.println(o); 

} }

Map&lt;String, List&gt; map = new HashMap&lt;String, List&gt;(); for (Map.Entry entry : map.entrySet()) { String key = entry.getKey().toString(); List&lt;String&gt; list= (List) entry.getValue(); for (String value : list) {

讯享网System.out.println(key </span>+ "====" +<span style="color: rgba(0, 0, 0, 1)"> value); 

} }


小讯
上一篇 2025-05-04 23:55
下一篇 2025-05-23 07:37

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/179745.html