• Android List<Map<String,String>转json(例子)


    package com.armslee.json.test.cases;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import net.sf.json.JSONArray;
    
    public class TestListMapJson {
    
        /**
         * @param Lavender
         */
        public static void main(String[] args) {
            Map<String,String> map1 = new HashMap<String,String>();
            Map<String,String> map2 = new HashMap<String,String>();
            Map<String,String> map3 = new HashMap<String,String>();
            List<Map<String,String>> list = new ArrayList<Map<String,String>>();
            map1.put("name","小明");
            map1.put("age","23");
            map1.put("sex","");
            list.add(map1);
    
            map2.put("name","小王");
            map2.put("age","24");
            map2.put("sex","");
            list.add(map2);
    
            map3.put("name","小张");
            map3.put("age","22");
            map3.put("sex","");
            list.add(map3);
            String jsonStr = JSONArray.fromObject(list).toString();
            
            JSONArray ja = JSONArray.fromObject(jsonStr);
            
            @SuppressWarnings({ "deprecation", "unchecked" })
            List<Map<String,String>> listBack = JSONArray.toList(ja, HashMap.class);
            
            System.out.println(listBack.size());
            int i = 0;
            for (Map<String, String> map : listBack) {
                i++;
                StringBuffer sb = new StringBuffer();
                sb.append(""+i+" 个MAP ---》");
                if(map.containsKey("name")){
                    sb.append(" Name:"+map.get("name"));
                }
                if(map.containsKey("age")){
                    sb.append(" Age:"+map.get("age"));        
                }
                if(map.containsKey("sex")){
                    sb.append(" Sex:"+map.get("sex"));
                }
                
                System.out.println(sb.toString());
            }
    
        }
    
    }
  • 相关阅读:
    Asp.net mvc validaterequest无效的问题
    News Master-DC and Marvel they are super heroes mother
    <<杰克.韦尔奇自传>>
    UIUC同学Jia-Bin Huang收集的计算机视觉代码合集
    6 个优秀的开源 OCR 光学字符识别工具
    应用OpenCV进行OCR字符识别
    心胸与格局
    Software: MPEG-7 Feature Extraction Library
    也谈LBP
    3个著名加密算法(MD5、RSA、DES)的解析
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5684667.html
Copyright © 2020-2023  润新知