zoukankan      html  css  js  c++  java
  • java实现读取json文件指定字段值

    使用场景

    现有一个大数据的json文件,每条数据有多层数据信息。现在想把其中某个字段提取并叠加计算。

    json文件格式

     1 {
     2   "MsgID":"111",
     3   "TaskData":{
     4   "TaskID":1,
     5   "RouteData":{
     6     "OrgPt":{"Angle":180...},
     7     "ExtendField":{"High":"580"...},
     8     "RoutePts":[],
     9     "NumOfPoints":231,
    10      "BIMMapCode":"P000011"
    11   }
    12   }
    13 }
    14 
    15 
    16 其中,第三层的字段RoutePts数组内容如下:
    17 {
    18         "Order":1,
    19         "Angle":90,
    20         "X":11969,
    21         "WorkState":1,
    22         "Y":5270,
    23         "Z":0,
    24         "WorkData":[
    25           {
    26             "RS":25,
    27             "PointCount":4,
    28             "Type":1,
    29             "WallNumber":2,
    30             "Points":[
    31               [
    32                 650,
    33                 -310,
    34                 2271
    35               ],
    36               [
    37                 650,
    38                 -310,
    39                 1575
    40               ],
    41               [
    42                 650,
    43                 -310,
    44                 869
    45               ],
    46               [
    47                 650,
    48                 -310,
    49                 275
    50               ]
    51             ],
    52             "BeamInfo":[],
    53             "WallThickness":200,
    54             "RL":25,
    55             "Exterior":0
    56           }
    57         ]
    58       },
    59 {
    60         "Order":2,
    61         "Angle":90,
    62         "X":12470,
    63         "WorkState":0,
    64         "Y":5270,
    65         "Z":0,
    66         "WorkData":[]
    67       }

    我们要提取RoutePts里面的PointCount字段并叠加计算,因为存在有些值为空,所以要筛选该字段。

    导入依赖

    在pom.xml文件里加上fastjson

    代码实现

    读取本地json文件的方法

     1 //读取json文件
     2     public static String readJsonFile(String fileName) throws IOException {
     3         String jsonStr="";
     4         try{
     5             File jsonFile=new File(fileName);
     6             FileReader fileReader=new FileReader(jsonFile);
     7             Reader reader=new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
     8             int ch=0;
     9             StringBuffer sb=new StringBuffer();
    10             while ((ch=reader.read())!=-1){
    11                 sb.append((char)ch);
    12             }
    13             fileReader.close();
    14             reader.close();
    15             jsonStr=sb.toString();
    16             return jsonStr;
    17         } catch (FileNotFoundException e) {
    18             e.printStackTrace();
    19             return null;
    20         }
    21     }

    读取指定字段

    先把json文件放在resourses下面(直接复制到resourses)

     1 public static void main(String[]args) throws ParseException, IOException {
     2         String path=luogandong.class.getClassLoader().getResource("package.json").getPath();
     3         String s=readJsonFile(path);  //读取resource里面json文件
     4         JSONObject jobj = JSON.parseObject(s);  //获取整个json文件对象
     5         System.out.println("MsgID"+jobj.get("MsgID"));
     6         JSONObject s1=jobj.getJSONObject("TaskData");  //获取第二层TaskData对象
     7         JSONObject s2=s1.getJSONObject("RouteData");   //获取第三层RouteData对象
     8         Integer s3=(Integer) s1.get("TaskID");
     9         Integer s4=(Integer)s2.get("NumOfPoints");
    10         String s7=(String)s2.get("BIMMapCode") ;
    11         System.out.println("TaskID"+s3);
    12         System.out.println("NumOfPoints"+s4);
    13         System.out.println("BIMMapCode"+s7);
    14 
    15         JSONArray links=s2.getJSONArray("RoutePts");  //获取RoutePts数组对象
    16 
    17         System.out.println("输入起始站点:");
    18         Scanner input1=new Scanner(System.in);
    19         int h=input1.nextInt();
    20 
    21         System.out.println("输入结束站点:");
    22         Scanner input=new Scanner(System.in);
    23         int j=input.nextInt();
    24 
    25         int s5=0;
    26 
    27         for(int i=h-1;i<j;i++){
    28             JSONArray links1=links.getJSONObject(i).getJSONArray("WorkData");  //获取第几个站点的workdata数组对象
    29             if(links1.isEmpty()||links1.size()<1){
    30                 System.out.println("[info]----"+(i+1)+"站点是非工作点,没有孔洞"+"----");   //判断数组是否为空,空则不计算
    31             }else {
    32                 int s6=links1.getJSONObject(0).getIntValue("PointCount");  //获取数组里面第二个字段的值
    33                 //int s8=s6.getIntValue();
    34                 s5+=s6;   //累计叠加
    35                 System.out.println("[info]----"+(i+1)+"站点有"+s6+"孔洞,"+h+"站点到"+(i+1)+"站点一共有"+s5+"个孔洞"+"----");
    36             }
    37 
    38         }
    39     }

    运行结果

  • 相关阅读:
    java匿名对象
    Java面向对象详解
    Java语言基本语法
    Win7下JDK环境变量的设置
    LeetCode-Shortest Word Distance
    LeetCode-Count Complete Tree Nodes
    LeetCode-Palindrome Pairs
    LeetCode- Implement Trie (Prefix Tree)
    LeetCode-Lowest Common Ancestor of a Binary Tre
    LeetCode- Binary Tree Longest Consecutive Sequence
  • 原文地址:https://www.cnblogs.com/datacenter/p/15594880.html
Copyright © 2011-2022 走看看