zoukankan      html  css  js  c++  java
  • 把json按特定格式读出

      1 {
      2  "id": "tactic_1352426077777",
      3  "name": "测试策略",
      4  "protocols_data": [
      5   {
      6    "treatyname": "全部协议组",
      7    "priority": 1,
      8    "dataval": {
      9     "range": "全时段",
     10     "controls": {
     11      "controls": [
     12       {
     13        "id": " 1",
     14        "name": "信号干扰",
     15        "color": "#569"
     16       },
     17       {
     18        "id": " 3",
     19        "name": "提示音干扰",
     20        "color": "#762"
     21       }
     22      ],
     23      "data": [
     24       {
     25        "id": "0-0",
     26        "control": "0",
     27        "color": "#569"
     28       },
     29       {
     30        "id": "0-0",
     31        "control": "2",
     32        "color": "#569"
     33       },
     34      {
     35        "id": "0-0",
     36        "control": " ",
     37        "color": "#569"
     38       },
     39       {
     40        "id": "6-47",
     41        "control": "3",
     42        "color": "#569"
     43       },
     44       {
     45        "id": "0-0",
     46        "control": " ",
     47        "color": "#569"
     48       },
     49        {
     50        "id": "0-0",
     51        "control": "1",
     52        "color": "#569"
     53       },
     54       {
     55        "id": "0-0",
     56        "control": "2",
     57        "color": "#569"
     58       },
     59      {
     60        "id": "0-0",
     61        "control": " ",
     62        "color": "#569"
     63       },
     64       {
     65        "id": "6-47",
     66        "control": "3",
     67        "color": "#569"
     68       },
     69       {
     70        "id": "0-0",
     71        "control": " ",
     72        "color": "#569"
     73       },
     74         {
     75        "id": "0-0",
     76        "control": "1",
     77        "color": "#569"
     78       },
     79       {
     80        "id": "0-0",
     81        "control": "2",
     82        "color": "#569"
     83       },
     84      {
     85        "id": "0-0",
     86        "control": " ",
     87        "color": "#569"
     88       },
     89       {
     90        "id": "6-47",
     91        "control": "3",
     92        "color": "#569"
     93       },
     94       {
     95        "id": "0-0",
     96        "control": " ",
     97        "color": "#569"
     98       },
     99        {
    100        "id": "0-0",
    101        "control": "1",
    102        "color": "#569"
    103       },
    104       {
    105        "id": "0-0",
    106        "control": "2",
    107        "color": "#569"
    108       },
    109      {
    110        "id": "0-0",
    111        "control": " ",
    112        "color": "#569"
    113       },
    114       {
    115        "id": "6-47",
    116        "control": "3",
    117        "color": "#569"
    118       },
    119       {
    120        "id": "0-0",
    121        "control": " ",
    122        "color": "#569"
    123       },
    124       {
    125        "id": "0-0",
    126        "control": "1",
    127        "color": "#569"
    128       },
    129       {
    130        "id": "0-0",
    131        "control": "2",
    132        "color": "#569"
    133       },
    134      {
    135        "id": "0-0",
    136        "control": " ",
    137        "color": "#569"
    138       },
    139       {
    140        "id": "6-47",
    141        "control": "3",
    142        "color": "#569"
    143       },
    144       {
    145        "id": "0-0",
    146        "control": " ",
    147        "color": "#569"
    148       },
    149        {
    150        "id": "0-0",
    151        "control": "1",
    152        "color": "#569"
    153       },

    上面是一段json的片段,我们这个程序演示的是把data数组的control:X 读出来,这个X有可能是null等。

     1 public class json1 {
     2 
     3     public static List<String> readfile() // 读取文件
     4     {
     5         List<String> control = new ArrayList<String>();
     6         try {
     7             MultiMap mm = new MultiMap();
     8             FileReader fr = new FileReader("json1");
     9             BufferedReader br = new BufferedReader(fr);
    10             String str = null;
    11             while ((str = br.readLine()) != null) {
    12                 String[] ss = null;
    13                 if (str.contains(":")) {
    14                     ss = str.trim().split(":");
    15                     mm.put(ss[0].trim(), ss[1].trim().replace(",", ""));
    16                 }
    17             }
    18             control = mm.get("\"control\"");
    19 
    20         } catch (Exception e) {
    21             e.printStackTrace();
    22         }
    23         return control;
    24     }
    25 
    26     private static Map<String, String> controltomap(List name,
    27             List<String> value)// 以键值对的形式存储
    28     {
    29         Map<String, String> map = new HashMap<String, String>();
    30         for (int i = 0; i < name.size(); i++) {
    31             if (!name.get(i).equals("\" \"")) {
    32                 String va = "";
    33                 for (int j = 0; j < value.size(); j++) {
    34                     if (!value.get(j).equals(name.get(i))) {
    35                         va += "0";
    36                     } else {
    37                         va = va + "1";
    38                     }
    39                 }
    40                 map.put((String) name.get(i), va);
    41             }
    42         }
    43         return map;
    44     }
    45 
    46     // 除去重复键
    47     private static List removeDuplicate(List<String> list) {
    48         HashSet h = new HashSet(list);
    49         List<String> lis = new ArrayList<String>();
    50         lis.addAll(h);
    51         return lis;
    52     }
    53 
    54     public static void main(String[] args) {
    55         List<String> control1 = new ArrayList<String>(); // 获取文件的所有值
    56         List<String> control2 = new ArrayList<String>(); // 获取文件的关键值
    57         control1 = readfile();
    58         control2 = removeDuplicate(control1);
    59         Map<String, String> ma = controltomap(control2, control1);
    60         System.out.println(control1);
    61         for (int i = 0; i < control2.size(); i++) {
    62             if (!control2.get(i).equals("\" \"")) {
    63                 System.out.println(control2.get(i));
    64                 System.out.println(ma.get(control2.get(i)));
    65             }
    66         }
    67         
    68     }

    后续会把转换成XML的代码上传!!!!

  • 相关阅读:
    C# .Net基础知识点解答
    依赖注入框架Autofac的简单使用
    Linq表达式、Lambda表达式你更喜欢哪个?
    C#抽象类、接口、虚函数和抽象函数
    MVC面试问题与答案
    并发 并行 同步 异步 多线程的区别
    .Net中的控制翻转和依赖注入
    解析ASP.NET WebForm和Mvc开发的区别
    测试与代码质量
    netty 同步调用
  • 原文地址:https://www.cnblogs.com/liangdelin/p/2789470.html
Copyright © 2011-2022 走看看