您的位置:首页 > 运维架构 > Linux

读取linux服务器内带格式文件,转为json字符串

2017-10-26 15:12 721 查看
版权声明:本文为博主原创文章,转载请附上博主原地址 https://blog.csdn.net/zhaosongbin/article/details/87918327

 

 

工具类方法:ReadTextUtil

 

[code] 1 package com.dc.health.platform.common.utils;
2
3 import com.alibaba.fastjson.JSONObject;
4
5 import java.util.ArrayList;
6 import java.util.List;
7
8 public class ReadTextUtil {
9
10     public static JSONObject ReadTextToJson(String path) {
11         if (path == null || "".equals(path))
12             return null;
13         List<JSONObject> jsObjectList = new ArrayList<>();
14         List<String> flist = SSHUtil.execCom("cat " + path);
15         if (flist != null && flist.size() > 0){
16             for (String s :  flist) {
17                 JSONObject jsObject = new JSONObject();
18                 String[] str = s.split("\\s+");
19                 if (str != null && str.length > 1){
20                     jsObject.put(str[0], str[1]);
21                 }else if (str != null && str.length > 0){
22                     jsObject.put(str[0], "");
23                 }
24                 jsObjectList.add(jsObject);
25             }
26         }
27         JSONObject json = new JSONObject();
28         json.put("data", jsObjectList);
29         return json;
30     }
31 }

 

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: