python - 几个json的整合
迷茫
迷茫 2017-04-17 17:29:53
0
2
313

第一个文件

{
"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

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

membalas semua(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"]);
Ty80

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"];//删除我们所不需要的元素

}

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

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!