美文网首页
Json格式化解析

Json格式化解析

作者: 大河马爱吃草 | 来源:发表于2017-04-25 23:42 被阅读0次

    近期一个项目需要将解密后的json文件转换为有格式的txt文件,尝试写了一个demo,放代码。
    <pre>
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;

    /**

    • Created by hexz on 2017/4/25.
      */
      public class Main {
      public static void main(String[] args) throws Exception{
      String path = "C:\Users\hexz\Desktop\testdata\dakehu_decrypt.json";
      FileInputStream fileInputStream = new FileInputStream(path);
      InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
      BufferedReader reader =new BufferedReader(inputStreamReader);
      String tempString = null;
      StringBuilder laststr = new StringBuilder();
      while ((tempString = reader.readLine()) != null){
      laststr.append(jsonParse(tempString));
      }
      reader.close();
      System.out.println(laststr);
      }
      public static String jsonParse(String jsonfile) throws Exception{
      StringBuilder jsonSB = new StringBuilder();
      String jsonString = "";

       JSONObject jsonObject = (JSONObject) JSON.parse(jsonfile);
      
       JSONArray jsonArray = jsonObject.getJSONArray("assetInfos");
       for (int i =0; i < jsonArray.size(); i++){
           jsonSB.append(jsonObject.getString("licenseCustomerId"));
           jsonSB.append("\t");
           jsonSB.append(jsonObject.getString("branchName"));
           jsonSB.append("\t");
           jsonSB.append(jsonObject.getString("crmCustomerId"));
           jsonSB.append("\t");
      
           JSONObject assetInfos =jsonArray.getJSONObject(i);
           String assetFullNumber = assetInfos.getString("assetFullNumber");
           if(assetFullNumber == null) {
               assetFullNumber = "null";
           }
           jsonSB.append(assetFullNumber);
           jsonSB.append("\t");
      
           String assetShellNumber = assetInfos.getString("assetShellNumber");
           if(assetInfos.getString("assetShellNumber") == "") {
               assetShellNumber = "null";
           }
           jsonSB.append(assetShellNumber);
           jsonSB.append("\t");
      
           String assetLicenseType = assetInfos.getString("assetLicenseType");
           if(assetInfos.getString("assetLicenseType") == "") {
               assetLicenseType = "null";
           }
           jsonSB.append(assetLicenseType);
           jsonSB.append("\t");
           String assetCreateTime = assetInfos.getString("assetCreateTime");
           if(assetInfos.getString("assetCreateTime") == "") {
      
               assetCreateTime = "null";
           }
           jsonSB.append(assetCreateTime);
           jsonSB.append("\t");
           String assetUpdateTime = assetInfos.getString("assetUpdateTime");
           if(assetInfos.getString("assetUpdateTime") == "") {
      
               assetUpdateTime = "null";
           }
           jsonSB.append(assetUpdateTime);
           jsonSB.append("\t");
           String assetEffectiveStartDate = assetInfos.getString("assetEffectiveStartDate");
           if(assetInfos.getString("assetEffectiveStartDate") == "") {
      
               assetEffectiveStartDate = "null";
           }
           jsonSB.append(assetEffectiveStartDate);
           jsonSB.append("\t");
           String assetEffectiveEndDate = assetInfos.getString("assetEffectiveEndDate");
           if(assetInfos.getString("assetEffectiveEndDate") == "") {
      
               assetEffectiveEndDate = "null";
           }
           jsonSB.append(assetEffectiveEndDate);
           jsonSB.append("\t");
           String assetLimitConcurrent = assetInfos.getString("assetLimitConcurrent");
           if(assetInfos.getString("assetLimitConcurrent") == "") {
      
               assetLimitConcurrent = "null";
           }
           jsonSB.append(assetLimitConcurrent);
           jsonSB.append("\t");
           String assetAssignStatus = assetInfos.getString("assetAssignStatus");
           if(assetInfos.getString("assetAssignStatus") == "") {
      
               assetAssignStatus = "null";
           }
           jsonSB.append(assetAssignStatus);
           jsonSB.append("\t");
           String employeeName = assetInfos.getString("employeeName");
           if(assetInfos.getString("employeeName") == "") {
      
               employeeName = "null";
           }
           jsonSB.append(employeeName);
           jsonSB.append("\t");
           String employeeEmail = assetInfos.getString("employeeEmail");
           if(assetInfos.getString("employeeEmail") == "") {
      
               employeeEmail = "null";
           }
           jsonSB.append(employeeEmail);
           jsonSB.append("\t");
           String employeeCellNumber = assetInfos.getString("employeeCellNumber");
           if(assetInfos.getString("employeeCellNumber") == "") {
      
               employeeCellNumber = "null";
           }
           jsonSB.append(employeeCellNumber);
           jsonSB.append("\t");
           String employeeUpdateTime = assetInfos.getString("employeeUpdateTime");
           if(assetInfos.getString("employeeUpdateTime") == "") {
      
               employeeUpdateTime = "null";
           }
           jsonSB.append(employeeUpdateTime);
           jsonSB.append("\t");
           String locked = assetInfos.getString("locked");
           if(assetInfos.getString("locked") == "") {
      
               locked = "null";
           }
           jsonSB.append(locked);
           jsonSB.append("\t");
           String employeeGlobalId = assetInfos.getString("employeeGlobalId");
           if(assetInfos.getString("employeeGlobalId") == "") {
      
               employeeGlobalId = "null";
           }
           jsonSB.append(employeeGlobalId);
      
           jsonSB.append("\t");
           jsonSB.append("\r\n");
       }
       jsonString = jsonSB.toString();
       return jsonString;
      

      }
      }

    </pre>

    相关文章

      网友评论

          本文标题:Json格式化解析

          本文链接:https://www.haomeiwen.com/subject/zlbbzttx.html