提取和替换json数据

时间:2018-12-27 15:45:37

标签: python json web-scraping

我想替换并格式化json文件中列出内容的方式。 我要返回的当前结构如下:

{"success": "OK", "message": "DEPARTURES_FOUND", "data": 
   {"departures": 
      [{"id": 58445, "itinerary_id": 1151, "codcorsa": "CASO1812271635NG", "ship_description": "Fast Ferry", "delay": null, "is_suspended": null, "notes": null, "type_id": 2, "departurePort": "CAPRI", "arrivalPort": "SORRENTO", "departureDate": "2018-12-27 16:35", "arrivalDate": "2018-12-27 16:55"},
      {"id": 58425, "itinerary_id": 1045, "codcorsa": "PRIC1812271620NS", "ship_description": "Fast Ferry", "delay": null, "is_suspended": null, "notes": null, "type_id": 2, "departurePort": "PROCIDA", "arrivalPort": "ISCHIA CASAMICCIOLA", "departureDate": "2018-12-27 17:05", "arrivalDate": "2018-12-27 17:15"}

我想使用python删除第一行并以以下结构显示时间表。 “ IS_SUSPENDED”:null替换为“ STATUS”:“活动”

[{"DEPARTURE DATE": "2018-12-27 16:35", ARRIVAL DATE": "2018-12-27 16:55","DEPARTURE PORT": "Capri", "ARRIVAL PORT": "Sorrento", "STATUS": "active"}

2 个答案:

答案 0 :(得分:0)

Python具有对JSON的本地支持,只需在代码顶部添加import json并使用json.loads()来读取数据。

这里是一个例子:

with open("data_file.json", "r") as read_file:
    data = json.load(read_file)

或者这就是您描述的一些代码修改输入数据的样子。请注意,json_string将更改为已加载的文件或API响应

#!python3

import json

def cleanText( text ):
    return text.lower().capitalize()

json_string = '''
{
    "success": "OK", 
    "message": "DEPARTURES_FOUND", 
    "data": {
        "departures": [
            {
                "id": 58445, 
                "itinerary_id": 1151,
                "codcorsa": "CASO1812271635NG", 
                "ship_description": "Fast Ferry", 
                "delay": null, 
                "is_suspended": null, 
                "notes": null, 
                "type_id": 2, 
                "departurePort": "CAPRI",
                "arrivalPort": "SORRENTO",
                "departureDate": "2018-12-27 16:35",
                "arrivalDate": "2018-12-27 16:55"
            },
            {
                "id": 58425, 
                "itinerary_id": 1045,
                "codcorsa": "PRIC1812271620NS",
                "ship_description": "Fast Ferry",
                "delay": null,
                "is_suspended": null,
                "notes": null,
                "type_id": 2,
                "departurePort": "PROCIDA",
                "arrivalPort": "ISCHIA CASAMICCIOLA",
                "departureDate": "2018-12-27 17:05",
                "arrivalDate": "2018-12-27 17:15"
            }
        ]
    }
}
'''

data = json.loads( json_string )
cleanData = []

for departure in data['data']['departures']:
    cleanData.append({
        "DEPARTURE DATE": departure['departureDate'], 
        "ARRIVAL DATE": departure['arrivalDate'],
        "DEPARTURE PORT": cleanText( departure['departurePort'] ),
        "ARRIVAL PORT": cleanText( departure['arrivalPort'] ),
        "STATUS": "suspended" if departure['is_suspended'] else "active"
    })

print( json.dumps( cleanData ) )

json库文档:https://docs.python.org/3.7/library/json.html

答案 1 :(得分:0)

尝试

$nameArr = [];
$arr = json_decode($theObject);

foreach ($arr as $name) {
    array_push($nameArr, $name->name);
}