开发者社区> 问答> 正文

有关于 几个json的整合 问题

第一个文件

{
"data": [
    {
      "id": "123456",
      "create_time": "2016-03-28 11:41:00",
      "phone": "138888****",
      "name": "路人甲",
    },
    {
      "id": "456789",
      "create_time": "2016-03-30 11:41:00",
      "phone": "138888****",
      "name": "炮兵灰",
    },
    .....
    ........
  ]
}

第二个文件

{
"data": [
      "id": "123456",
      "driver": "2016-03-28",
      "work": "你好",
      "type": "A",
    },
    {
      "id": "456789",
      "driver": "2016-03-28",
      "work": "不好",
      "type": "B",
    },
    .....
    ........
  ]
}

第三个文件

{
"data": [
      "id": "123456",
      "service_distance": "111.22",
      "amount": "3.333",
      "strive_cnt": "0",
    },
    {
      "id": "123456",
      "service_distance": "111.22",
      "amount": "3.333",
      "strive_cnt": "0",
    },
    .....
    ........
  ]
}

这个时候
我应该怎么写脚本
让几个特定的json参数排序好 集合在一起
生成一个全新的json

比如我只要其中的 id name phone work amount
重组在一个json

展开
收起
爵霸 2016-03-05 08:16:33 1963 0
1 条回答
写回答
取消 提交回答
  • import json
    
    jsonOne = {
        "data": [
            {
                "id": "123456",
                "create_time": "2016-03-28 11:41:00",
                "phone": "138888****",
                "name": "aaa"
            },
            {
                "id": "456789",
                "create_time": "2016-03-30 11:41:00",
                "phone": "138888****",
                "name": "bbb"
            }
        ]
    }
    
    jsonTwo = {
        "data": [
            {
                "id": "123456",
                "driver": "2016-03-28",
                "work": "work1",
                "type": "A"
            },
            {
                "id": "456789",
                "driver": "2016-03-28",
                "work": "work2",
                "type": "B"
            }
        ]
    }
    
    newJson = {
        "data": []
    }
    
    for index,item in enumerate(jsonOne['data']):
        newObj = {
            "id": item["id"],
            "create_time": item["create_time"],
            "phone": item["phone"],
            "name": item["name"],
            "driver": jsonTwo["data"][index]["driver"],
            "work": jsonTwo["data"][index]["work"],
            "type": jsonTwo["data"][index]["type"]
    
        };
        newJson["data"].append(newObj)
    
    
    print(newJson["data"]);
    
    2019-07-17 18:52:49
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载