我有一个json输入
{
"registryNo":2222904913,
"resgistrySource":"C22",
"DateTime":"None",
"Payments":[{
"Paymentdetail":[
{
"amount":359.95,
"currencyCode":"GBP",
需要生成一个布尔输出来检查货币代码字段是否具有值GBP。如果是,则将货币值打印为"True",否则打印为"False">
这是我写的
if "currencycode" in datastore(["Payments"][0]["paymentdetail"]) == "GBP":
print("currencyvalue") = "True" else "FALSE"
--这似乎不起作用。
如果我能很好地理解你的问题你可以尝试这样做:
if "currencycode" in datastore(["Payments"][0]["paymentdetail"][0]):
if datastore(["Payments"][0]["paymentdetail"][0]["currencycode"]) == "GBP":
print("currencyvalue = True")
else:
print("currencyvalue = false")
如果您只想打印"True"或"False",请执行以下操作:
if "currencycode" in datastore(["Payments"][0]["paymentdetail"][0]):
print(datastore(["Payments"][0]["paymentdetail"][0]["currencycode"]))
对于时间戳,您可以这样做:
from datetime import datetime
dt_object = datetime.fromtimestamp(your_valid_timestamp)
print(dt_object)
由于==
运算符本身返回True
或False
值,因此无需再次分配它。
currencycalue = datastore['Payments'][0]['Paymentdetail'][0]['currencyCode']=="GBP"
print(currencyvalue)