一个json文件中有一个内置的三重的字典,还有一个excel表格如何重json中提取到excel对应的json数据内容,给出代码示例

一个json文件中有一个内置的三重的字典,还有一个excel表格如何重json中提取到excel对应的json数据内容,给出代码示例

import pandas as pd
import json

with open('data.json') as f:
    data = json.load(f)

output_df = pd.DataFrame(columns=['Outer Key', 'Middle Key', 'Inner Key', 'Value'])

for outer_key, outer_dict in data.items():
    for middle_key, middle_dict in outer_dict.items():
        for inner_key, value in middle_dict.items():
            output_df = output_df.append({'Outer Key': outer_key,
                                          'Middle Key': middle_key,
                                          'Inner Key': inner_key,
                                          'Value': value}, ignore_index=True)


output_df.to_excel('output.xlsx', index=False)

  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/1035061
  • 你也可以参考下这篇文章:强大易用的Excel转Json工具
  • 除此之外, 这篇博客: json格式数据转化为excel表格中数据中的 二、json转化为excel表格数据 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • json格式确实直观,但是有时候我们需要对数据进行处理,此时,我们常用的就是excel表格。

    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    import org.apache.commons.io.FileUtils;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import java.io.*;
    
    public class Test {
        public static void main(String[] args) {
            try{
                String filePath="这里写导出excel文件的路径";
                //读取excel文件
                InputStream is = new FileInputStream(filePath);
                //创建excel工作薄
                XSSFWorkbook workbook = new XSSFWorkbook(is);
                //创建一个工作表sheet
                XSSFSheet sheet = workbook.getSheetAt(0);
    
                //创建第一行表头数据
                XSSFRow row1 = sheet.createRow(0);
                XSSFCell cell_first1 = row1.createCell(0);
                cell_first1.setCellValue("userName");
                XSSFCell cell_second1 = row1.createCell(1);
                cell_second1.setCellValue("displayName");
                XSSFCell cell_third1 = row1.createCell(2);
                cell_third1.setCellValue("roles__value");
                XSSFCell cell_forth1 = row1.createCell(3);
                cell_forth1.setCellValue("roles__displayName");//这一段主要是想要生成的表格的样式
    
                //读取json数据并解析
                JSONArray jsonData = getJsonData();
                int colunm = 1;//行数计算器,从第二行开始写入
                for(int i=0;i<jsonData.size();i++) {
                    JSONObject firstModularObject = jsonData.getJSONObject(i);
                    String username=firstModularObject.get("userName").toString();
                    try{
                        String displayname=firstModularObject.get("displayName").toString();
                        JSONArray secondModular=(JSONArray)firstModularObject.get("roles");
    
                        if(secondModular.size()>0){
                            //第二级目录
                            for(int l=0;l<secondModular.size();l++) {
                                JSONObject secondModularJSONObject = secondModular.getJSONObject(l);
                                String value=secondModularJSONObject.get("value").toString();
                                String displayName2=secondModularJSONObject.get("displayName").toString();
    
                                XSSFRow row = sheet.createRow(colunm);
                                XSSFCell cell_first = row.createCell(0);
                                cell_first.setCellValue(username);
                                XSSFCell cell_second = row.createCell(1);
                                cell_second.setCellValue(displayname);
                                XSSFCell cell_third = row.createCell(2);
                                cell_third.setCellValue(value);
                                XSSFCell cell_forth = row.createCell(3);
                                cell_forth.setCellValue(displayName2);
    
                                colunm++;
                            }
                        }else{
                            XSSFRow row = sheet.createRow(colunm);
                            XSSFCell cell_first = row.createCell(0);
                            cell_first.setCellValue(username);
                            XSSFCell cell_second = row.createCell(1);
                            cell_second.setCellValue(displayname);
    
                            colunm++;
                        }
                    }catch (NullPointerException e1) {
                        System.out.println("发生异常的原因为 :"+e1.getMessage());
                    }
                }//end for
                //保存到excel
                FileOutputStream outputStream = new FileOutputStream(filePath);
                workbook.write(outputStream);
                System.out.println("写入成功");
                outputStream.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 读取json数据并解析
         * @return
         * @throws IOException
         */
        public static JSONArray getJsonData() throws IOException {
            String filePath="这里写导入json文件的路径";
            File file = new File(filePath);
            JSONArray jsonArray = null;
            try {
                String input = FileUtils.readFileToString(file, "UTF-8");
                JSONObject jsonObject = JSONObject.fromObject(input);
                if (jsonObject != null) {
                    jsonArray = jsonObject.getJSONArray("Resources");
                }
            } catch (Exception e) {
                e.printStackTrace();
                jsonArray = null;
            }
            return jsonArray;
        }
    
    }
    
    
    

    这篇文章讲述的转化是很基础的部分,希望对大家有所帮助,如果文章有错误希望大家能为我指正。