I am trying to get a value from a response object in a dictionary, but I keep running into this error, am I wrong in thinking you __getitem__
is more commonly used for indexing in classes?
我试图从字典中的响应对象中获取值,但是我一直遇到这个错误,我认为__getitem__更常用于类中的索引是错误的吗?
Here is the code:
这是代码:
import json
import requests
from requests.auth import HTTPBasicAuth
url = "http://public.coindaddy.io:4000/api/"
headers = {'content-type': 'application/json'}
auth = HTTPBasicAuth('rpc', '1234')
payload = {
"method": "get_running_info",
"params": {},
"jsonrpc": "2.0",
"id": 0,
}
response = requests.post(url, data=json.dumps(payload), headers=headers, auth=auth)
print("respone is: ", response['result'])
2 个解决方案
#1
9
The response object is not a dictionary, you cannot use indexing on it.
响应对象不是字典,您不能对其使用索引。
If the API returns a JSON response, you need to use the response.json()
method to decode it to a Python object:
如果API返回JSON响应,则需要使用response.json()方法将其解码为Python对象:
data = response.json()
print("respone is: ", data['result'])
Note that you don't have to encode the request JSON data either; you could just use the json
argument to the request.post()
method here; this also sets the Content-Type header for you:
请注意,您不必对请求JSON数据进行编码;你可以在这里使用json参数到request.post()方法;这也为您设置Content-Type标头:
response = requests.post(url, json=payload, auth=auth)
Last but not least, if the API uses JSONRPC as the protocol, you could use the jsonrpc-requests
project to proxy method calls for you:
最后但并非最不重要的是,如果API使用JSONRPC作为协议,您可以使用jsonrpc-requests项目代理方法调用:
from jsonrpc_requests import Server
url = "http://public.coindaddy.io:4000/api/"
server = Server(url, auth=('rpc', '1234'))
result = server.get_running_info()
#2
1
Just change your source code a bit like this
只需更改源代码即可
response = requests.post(url, json=json.dumps(payload), headers=headers, auth=auth).json()
response = requests.post(url,json = json.dumps(payload),headers = headers,auth = auth).json()
print("respone is: ", response['result'].encode('utf-8'))
print(“respone is:”,response ['result']。encode('utf-8'))
It's true that response object alone can't be indexed instead for that purpose you need to return info in json format (in order parse response information ) which you can do so using json() and Here in order to get proper string you must encode it with utf-8 (other wise your output will be something like this -u'LikeThis)
确实单独的响应对象不能被索引而不是为了这个目的你需要以json格式返回信息(按顺序解析响应信息),你可以使用json()来实现这一点,为了获得正确的字符串,你必须编码它与utf-8(其他明智的你的输出将是这样的东西-u'LikeThis)
#1
9
The response object is not a dictionary, you cannot use indexing on it.
响应对象不是字典,您不能对其使用索引。
If the API returns a JSON response, you need to use the response.json()
method to decode it to a Python object:
如果API返回JSON响应,则需要使用response.json()方法将其解码为Python对象:
data = response.json()
print("respone is: ", data['result'])
Note that you don't have to encode the request JSON data either; you could just use the json
argument to the request.post()
method here; this also sets the Content-Type header for you:
请注意,您不必对请求JSON数据进行编码;你可以在这里使用json参数到request.post()方法;这也为您设置Content-Type标头:
response = requests.post(url, json=payload, auth=auth)
Last but not least, if the API uses JSONRPC as the protocol, you could use the jsonrpc-requests
project to proxy method calls for you:
最后但并非最不重要的是,如果API使用JSONRPC作为协议,您可以使用jsonrpc-requests项目代理方法调用:
from jsonrpc_requests import Server
url = "http://public.coindaddy.io:4000/api/"
server = Server(url, auth=('rpc', '1234'))
result = server.get_running_info()
#2
1
Just change your source code a bit like this
只需更改源代码即可
response = requests.post(url, json=json.dumps(payload), headers=headers, auth=auth).json()
response = requests.post(url,json = json.dumps(payload),headers = headers,auth = auth).json()
print("respone is: ", response['result'].encode('utf-8'))
print(“respone is:”,response ['result']。encode('utf-8'))
It's true that response object alone can't be indexed instead for that purpose you need to return info in json format (in order parse response information ) which you can do so using json() and Here in order to get proper string you must encode it with utf-8 (other wise your output will be something like this -u'LikeThis)
确实单独的响应对象不能被索引而不是为了这个目的你需要以json格式返回信息(按顺序解析响应信息),你可以使用json()来实现这一点,为了获得正确的字符串,你必须编码它与utf-8(其他明智的你的输出将是这样的东西-u'LikeThis)