Python - 有问题的 json 响应

标签 python json

我对 python 非常陌生,并尝试解析看似 JSON 数据。

import requests
import json

URL = "http://myip/cgi-bin/lun/luci/;stok=/login?form=login"

payload = {
'operation': 'login',
'username': 'someuser',
'password': 'somepassword'
}

session = requests.session()
r = requests.post(URL, data=payload)

print (r.json())

我得到的返回是:

{u'data': {u'stok': u'0c2f16c954cd2cafccfaf1bf50000ac6'}, u'success': True}

我需要在单引号中使用字母/数字的随机集合0c2f16c954cd2cafccfaf1bf50000ac6(它每次都会改变)。我在此回复中没有看到任何双引号。所以有几个问题:

  1. 我在响应中没有看到任何双引号。这还是json吗?
  2. 将随机字符的中间部分分配给变量以便我可以在接下来需要构建的请求网址中使用它的最佳方法是什么?

最佳答案

I dont see any double quotes in the response. Is this still json?

JSON。 r.json() 将 JSON 响应反序列化为 Python dict,然后 print 打印该 dict

What would be the best way to assign that middle part of random characters to a variable so I can use it in a request url I need to build next?

a_variable = r.json()["data"]["stok"]

关于Python - 有问题的 json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53455460/

相关文章:

javascript - 如何剪切json字符串?

javascript - 发送 JSON 作为 post 的参数值

python - 解析 YAML 文件并替换为模板

Python获取列表中具有匹配属性的对象

python - 在哪里可以找到每个 python 关键字如何实现的官方列表?

python - sympy:规范化右侧为常数的线性等式

javascript - 找不到入口模块中的错误 : Error: Can't resolve 'babel-loader'

python - 尝试将 python 片段导入 C/C++(PI spigot 算法)

javascript - 在javascript中将Json时间戳转换为正常日期和时间

java - 如何使用 json 通过 REST API 更新 Jira 中的描述