Python 从 POST 方法中获取 JSON
我有一个 Android 应用程序,最初将一些 json 格式的字符串发布到 python cgi 脚本中,一切正常。问题是,当 json 对象包含列表时,python(使用 simplejson)在获取它们时仍然将它们视为一个大字符串,
这是在解析它之前到达 python 后 json 的文本转储:
{“问题1”:“[1,2,3]”,“名称”:“aaa”,“操作”:1,“问题2”:“[20,20,20]”,“任务”:“[1 task, 2 task, 3 task]","Description":""}
如果我们查看“Tasks”键,后面的列表显然是一个字符串,其中的元素全部被视为一个字符串(即每个元素周围没有引号)。 prob1 和 prob2 是一样的。动作、名称等都很好。我不确定这是否是 python 所期望的,但我猜不是?
为了防止 android 数据受到指责,我在 arraylist 的每个元素周围添加了引号,如下所示:
Tasks.add('"'+row.get(1).toString()+'"');而不是 Tasks.add(row.get(1).toString());
在网络服务器上,它现在被接收为
{“问题1”:“[1,2,3]”,“名称”:“aaa”,“操作”:1,“问题2”:“[20,20,20]”,“任务”:“[\ "1 任务\", \"2 任务\", \"3 任务\"]","描述":""}
但我仍然遇到同样的问题;当我在循环中迭代“Tasks”时,它会循环遍历每个单独的字符,就好像整个事情是一个字符串一样:/
因为我不知道json结构在到达Python之前应该是什么样子,我想知道它是否是一个探针,Android 发送数据或我的 python 解释它..虽然从该脚本的外观我一直猜测它是发送的。
在 Android 应用程序中,我发送一个包含“任务”的大 JSONObject 和关联的数组列表作为键值对之一......这是正确的吗?或者 JSONArray 应该在任何地方参与吗?
感谢大家的帮助,我对整个 JSON 事物以及 Android/Java 都是新手(而且也只是 Python 的新手..)。如果有人需要,我可以发布额外的代码,我只是不想将帖子加长太多
编辑:
当我添加
json_data=json_data.replace(r'"[','[')
json_data=json_data.replace(r']"',']')
json_data=json_data.replace(r'\"','"')
对于Python来说它可以工作!!!但这让我觉得有点令人讨厌,只是掩盖了裂缝。
I have an Android appthat originally posted some strings in json format to a python cgi script, which all worked fine. The problem is when the json object contains lists, then python (Using simplejson) when it gets them is still treating them as a big string
Here is a text dump of the json once it reaches python before I parse it:
{"Prob1":"[1, 2, 3]","Name":"aaa","action":1,"Prob2":"[20, 20, 20]","Tasks":"[1 task, 2 task, 3 task]","Description":""}
if we look at the "Tasks" key, the list after is clearly a single string with the elements all treated as one string (i.e. no quotes around each element). it's the same for prob1 and prob2. action, Name etc are all fine. I'm not sure if this is what python is expecting but I'm guessing not?
Just in case the android data was to blame i added quotes around each element of the arraylist like this:
Tasks.add('"'+row.get(1).toString()+'"'); instead of Tasks.add(row.get(1).toString());
On the webserver it's now received as
{"Prob1":"[1, 2, 3]","Name":"aaa","action":1,"Prob2":"[20, 20, 20]","Tasks":"[\"1 task\", \"2 task\", \"3 task\"]","Description":""}
but i still get the same problem; when i iterate through "Tasks" in a loop it's looping through each individual character as if the whole thing were a string :/
Since I don't know what the json structure should look like before it gets to Python I'm wondering whether it's a probem with the Android sending the data or my python interpreting it.. though from the looks of that script I've been guessing it's been the sending.
In the Android App I'm sending one big JSONObject containing "Tasks" and the associated arraylist as one of the key value pairs... is this correct? or should JSONArray be involved anywhere?
Thanks for any help everyone, I'm new to the whole JSON thing as well as to Android/Java (And only really a novice at Python too..). I can post additional code if anyone needs it, I just didn't want to lengthen the post too much
EDIT:
when I add
json_data=json_data.replace(r'"[','[')
json_data=json_data.replace(r']"',']')
json_data=json_data.replace(r'\"','"')
to the python it WORKS!!!! but that strikes me as a bit nasty and just papering over a crack..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
任务只是一个大字符串。要成为有效列表,它必须是 [“1 个任务”,“2 个任务”,“3 个任务”]
Same goes for Prob1 and Prob2. To be a valid list, the brackets should not be enclosed in quotes.
Tasks is just a big string. To be a valid list, it would have to be ["1 task", "2 task", "3 task"]
Same goes for Prob1 and Prob2. To be a valid list, the brackets should not be enclosed in quotes.