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     }

運行結果

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM