Java++:Json数组形式的字符串转为Map数组Map集合的⼏种
遍历⽅式
// Json数组形式的字符串转为Map数组
String strArr = "[{"name": "xx", "age": "16"},{"name": "yy", "age": "18"}]";
//第⼀种⽅式
List<Map<String,String>> listObjectFir = (List<Map<String,String>>) JSONArray.parse(strArr);
System.out.println("利⽤JSONArray中的parse⽅法来解析json数组字符串");
for(Map<String,String> mapList : listObjectFir){
for (Map.Entry entry : Set()){
System.out.println( Key()  + "  " +Value());
}
}
//第⼆种⽅式
List<Map<String,String>> listObjectSec = JSONArray.parseObject(strArr,List.class);
System.out.println("利⽤JSONArray中的parseObject⽅法并指定返回类型来解析json数组字符串");
for(Map<String,String> mapList : listObjectSec){
for (Map.Entry entry : Set()){
System.out.println( Key()  + "  " +Value());
}
}
//第三种⽅式
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());
}
}
//第五种⽅式java数组字符串转数组
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());
}
}
// map集合遍历
Map<String, Integer> tempMap = new HashMap<String, Integer>();
tempMap.put("a","12");
tempMap.put("b","34");
tempMap.put("c","56");
// 遍历⽅法⼀ hashmap entrySet() 遍历
Iterator it = Set().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = Key();
Object value = Value();
System.out.println("key=" + key + " value=" + value);
}
// 遍历⽅法⼆
for (Map.Entry<String, Integer> entry : Set()) {
String key = Key().toString();
String value = Value().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=" + (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=" + (o));
}
// java如何遍历Map <String, ArrayList> map = new HashMap <String,
// ArrayList>();
System.out.println("java  遍历Map <String, ArrayList> map = new HashMap<String, ArrayList>();");  Map<String, ArrayList> map = new HashMap<String, ArrayList>();
Set<String> keys = map.keySet();
Iterator<String> iterator = keys.iterator();
while (iterator.hasNext()) {
String key = ();
ArrayList arrayList = (key);
for (Object o : arrayList) {
System.out.println(o);
}
}
Map<String, List> map = new HashMap<String, List>();
for (Map.Entry entry : Set()) {
String key = Key().toString();
List<String> list= (List) Value();
for (String value : list) {
System.out.println(key + "====" + value);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。