几个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

阅读 5k
2 个回答
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"]);

for(var i = 0;i< a.length;i++){

for(var j = 0;j< b.length;j++){
    if(a[i].id == b[j].id){
        a[i]["work"] = b[j].work;//把b数组中想要的元素塞到a中
    }
}
for(var k = 0;k< c.length;k++){
    if(a[i].id == c[k].id){
        a[i]["amount"] = c[k].amount;//把c数组中想要的元素塞到a中
    }
}
delete a[i]["create_time"];//删除我们所不需要的元素

}

方法土一点,希望能帮到你。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
1 篇内容引用
推荐问题
宣传栏