我有一个JSON字典,可以由未知对象和数组组成,但我知道需要修改哪些键
我的代码将遍历JSON文件,如果它有某些键,比如“Date”,它将执行其他任务
该代码适用于外部键,但如果存在名为“Date”的嵌套键,则将跳过它
我需要密码逐行检查所有的钥匙
代码如下
def Converttimestamp(x):
format = '%Y-%m-%dT%H:%M:%S.%f'
print(x)
for e in x:
print(e)
for column in ['JournalDate','CreatedDateUTC','UpdatedDateUTC','Date','DueDate','DeliveryDate','ExpectedArrivalDate','DateUTC']:
if column in e:
ts = e[column]
print ("\n" + str(ts) + "\n")
ts_utc = re.split('\(|\)', ts)[1]
ts_utc = ts_utc[:ts_utc.find("+")]
e[column] = datetime.fromtimestamp(float(ts_utc)/1000).strftime(format)
print(str(e[column]))
return(e)
JSON口述:
x = {
"InvoiceID": "8930",
"InvoiceNumber": "Inv. 1",
"Reference": "",
"Payments": [
{
"PaymentID": "538",
"Date": "/Date(1406246400000+0000)/",
"Amount": 118.0,
"Reference": "",
"CurrencyRate": 1.0,
"HasAccount": false,
"HasValidationErrors": false
}
],
"AmountDue": 0.0,
"AmountPaid": 118.0,
"AmountCredited": 0.0,
"CurrencyRate": 1.0,
"Date": "/Date(1406246400000+0000)/",
"DueDate": "/Date(1406246400000+0000)/"
"UpdatedDateUTC": "/Date(1406246400000+0000)/"
}
结果JSON字典:
{
"InvoiceID": "8930",
"InvoiceNumber": "Inv. 1",
"Reference": "",
"Payments": [
{
"PaymentID": "538",
"Date": "/Date(1406246400000+0000)/",
"Amount": 118.0,
"Reference": "",
"CurrencyRate": 1.0,
"HasAccount": false,
"HasValidationErrors": false
}
],
"AmountDue": 0.0,
"AmountPaid": 118.0,
"AmountCredited": 0.0,
"CurrencyRate": 1.0,
"Date": "2014-06-30T02:00:00.000000",
"DueDate": "2014-06-30T02:00:00.000000"
"UpdatedDateUTC": "2014-12-15T14:08:51.843000"
}
Dates
位于Payments
键的list
内,因此必须通过访问列表内的字典来提取它李>资料
代码
使用和输出
相关问题 更多 >
编程相关推荐