[英]get name of "product" from json
这是 JSON 文本,我想获取“产品”的名称:
{"result": "success", "totalresults": 1, "startnumber": 0, "numreturned": 1, "orders": {"order": [{"id": 3267, "ordernum": 13424555, "userid": 2132, "contactid": 0, "requestor_id": 2173, "admin_requestor_id": 0, "date": "2022-08-05 16:39:18", "nameservers": "", "transfersecret": "", "renewals": "", "promocode": "", "promotype": "", "promovalue": "", "orderdata": "[]", "amount": "12.00", "paymentmethod": "usd", "invoiceid": 101, "status": "Active", "ipaddress": "111.111.111.111", "fraudmodule": "", "fraudoutput": "", "notes": "", "paymentmethodname": "Cryptocurrencies", "paymentstatus": "Paid", "name": "Max Vorenberg", "currencyprefix": "$", "currencysuffix": " USD", "frauddata": "", "validationdata": "", "lineitems": {"lineitem": [{"type": "product", "relid": 3648, "producttype": "Dedicated/VPS Server", "product": "Dedicated Server Windows", "domain": "chz", "billingcycle": "Monthly", "amount": "$12.00 USD", "status": "Active"}]}}]}}
这是我的代码:
data = {
'identifier':identifier,
'secret':secret,
'action': 'GetOrders',
'id':3267,
'responsetype':'json',
}
# sending post request and saving response as response object
r = requests.post(url = API_ENDPOINT, data = data)
data2 = r.json()
text_data2 = json.dumps(data2)
print(text_data2)
print(text_data2['product'])
但得到错误: "NameError: name 'string' is not defined"
这将为您的示例输入获得预期的结果:
data["orders"]["order"][0]["lineitems"]["lineitem"][0]["product"]
虽然,在我有[0]
的地方,它正在获得这些列表中的第一项。 您的示例在列表中只有一项,但如果还有更多,您将需要遍历它们以获得所需的结果。
这是基于您的示例数据:
data = {
'numreturned': 1,
'orders': {'order': [{'admin_requestor_id': 0,
'amount': '12.00',
'contactid': 0,
'currencyprefix': '$',
'currencysuffix': ' USD',
'date': '2022-08-05 16:39:18',
'frauddata': '',
'fraudmodule': '',
'fraudoutput': '',
'id': 3267,
'invoiceid': 101,
'ipaddress': '111.111.111.111',
'lineitems': {'lineitem': [{'amount': '$12.00 USD',
'billingcycle': 'Monthly',
'domain': 'chz',
'product': 'Dedicated '
'Server Windows',
'producttype': 'Dedicated/VPS '
'Server',
'relid': 3648,
'status': 'Active',
'type': 'product'}]},
'name': 'Max Vorenberg',
'nameservers': '',
'notes': '',
'orderdata': '[]',
'ordernum': 13424555,
'paymentmethod': 'usd',
'paymentmethodname': 'Cryptocurrencies',
'paymentstatus': 'Paid',
'promocode': '',
'promotype': '',
'promovalue': '',
'renewals': '',
'requestor_id': 2173,
'status': 'Active',
'transfersecret': '',
'userid': 2132,
'validationdata': ''}]},
'result': 'success',
'startnumber': 0,
'totalresults': 1
}
为了打破这一点,我首先获取包含此字典的data["orders]
:
{
'order': [{'admin_requestor_id': 0,
...
'lineitems': {'lineitem': [{'amount': '$12.00 USD',
'billingcycle': 'Monthly',
'domain': 'chz',
'product': 'Dedicated Server Windows',
'producttype': 'Dedicated/VPS Server',
'relid': 3648,
'status': 'Active',
'type': 'product'}]},
...
]
}
该字典只有一个键“orders”,其中包含一个包含单个字典的列表。 如果您希望它包含多个字典,那么您需要像这样循环遍历它:
for order in data["orders"]["order"]:
...
在...
中,您将有一个订单,如下所示:
{
'admin_requestor_id': 0,
...
'lineitems': {'lineitem': [{'amount': '$12.00 USD',
'billingcycle': 'Monthly',
'domain': 'chz',
'product': 'Dedicated Server Windows',
'producttype': 'Dedicated/VPS Server',
'relid': 3648,
'status': 'Active',
'type': 'product'}]},
...
}
这有一个键“lineitems”,它是一个包含单个键“lineitem”的字典,它是另一个字典列表,就像上面一样,你也可以迭代它。 到目前为止,我们的代码将变为:
for order in data["orders"]["order"]:
for item in order["lineitems"]["lineitem"]:
...
现在在内部循环中,我们有一个字典,如下所示:
{
'amount': '$12.00 USD',
'billingcycle': 'Monthly',
'domain': 'chz',
'product': 'Dedicated Server Windows',
'producttype': 'Dedicated/VPS Server',
'relid': 3648,
'status': 'Active',
'type': 'product'
}
这本字典有关键的“产品”,这是我们正在寻找的价值。 我们的迭代方法现在变为:
for order in data["orders"]["order"]:
for item in order["lineitems"]["lineitem"]:
print(item["product"])
当 json 被视觉格式化时,更容易理解为什么:
{
"result": "success",
"totalresults": 1,
"startnumber": 0,
"numreturned": 1,
"orders": {
"order": [
{
"id": 3267,
"ordernum": 13424555,
"userid": 2132,
"contactid": 0,
"requestor_id": 2173,
"admin_requestor_id": 0,
"date": "2022-08-05 16:39:18",
"nameservers": "",
"transfersecret": "",
"renewals": "",
"promocode": "",
"promotype": "",
"promovalue": "",
"orderdata": "[]",
"amount": "12.00",
"paymentmethod": "usd",
"invoiceid": 101,
"status": "Active",
"ipaddress": "111.111.111.111",
"fraudmodule": "",
"fraudoutput": "",
"notes": "",
"paymentmethodname": "Cryptocurrencies",
"paymentstatus": "Paid",
"name": "Max Vorenberg",
"currencyprefix": "$",
"currencysuffix": " USD",
"frauddata": "",
"validationdata": "",
"lineitems": {
"lineitem": [
{
"type": "product",
"relid": 3648,
"producttype": "Dedicated/VPS Server",
"product": "Dedicated Server Windows",
"domain": "chz",
"billingcycle": "Monthly",
"amount": "$12.00 USD",
"status": "Active"
}
]
}
}
]
}
}
您要查找的产品值嵌套在对象集合和 arrays 下,因此您需要更复杂的方法来解析值。
在最简单的情况下,您可以通过data2["orders"]["order"][0]["lineitems"]["lineitem"][0]["product"]
访问它,但这假设所有对象和arrays 存在,它们包含值,每个数组中的第一项是您想要的。 go 那里没有错。 ;)
此外,您可能希望在请求等周围添加一些尝试块,就好像它们失败它们会抛出异常一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.