zoukankan      html  css  js  c++  java
  • JSON循环遍历解析

    使用递归方式遍历JSON,解析JSON用的是:net.sf.json, alibaba.fastjson测试可用

     1  @Test
     2     public void test() {
     3         String json = "{}";
     4         test2((Map) JSONObject.fromObject(json));
     5     }
     6 
     7     private void test1(Map.Entry<String, Object> entry) {
     8         if (entry.getValue() instanceof JSONArray) {
     9             System.out.println("key=" + entry.getKey());
    10             List<Map<String, Object>> list = (List<Map<String, Object>>) entry.getValue();
    11             for (Map<String, Object> map : list) {
    12                 test2(map);
    13             }
    14         } else if (entry.getValue() instanceof JSONObject) {
    15             System.out.println("key=" + entry.getKey());
    16             Map jsonMap = (Map) JSONObject.fromObject(entry.getValue());
    17             test2(jsonMap);
    18         } else {
    19             System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
    20         }
    21     }
    22 
    23     private void test2(Map map) {
    24         Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
    25         while (it.hasNext()) {
    26             Map.Entry<String, Object> en = it.next();
    27             test1(en);
    28         }
    29     }
  • 相关阅读:
    ios 动画与2D、3D绘图
    ios UI设计与开发 按钮、图标和图片
    算法基础
    快速排序
    ios网络开发 同步下载和异步下载
    用C#调用Execl
    SQL函数大全
    出差
    窗体间的互操作
    垂直滚动条代码
  • 原文地址:https://www.cnblogs.com/xu-xiao/p/8133910.html
Copyright © 2011-2022 走看看