duancan2539 2018-07-03 04:51
浏览 168
已采纳

在PHP中处理嵌套的JSON POST请求

I am trying to have a python client send a post request which contains nested JSON like such

{"nested":{"field1":"response1", "field2":"response2"}}

My python code is here

from urllib.parse import urlencode
from urllib.request import Request, urlopen

url="http://localhost/api/vscore.php"
post_fields={"nested":{"field1":"response1", "field2":"response2"}}

request = Request(url, urlencode(post_fields).encode())
json = urlopen(request).read().decode()
print(json)

PHP code:

print_r($_POST["nested"]);

returns

{'field2': 'response2', 'field1': 'response1'}

but when I try to access "field1" with $_POST["nested"]["field1"], it returns this:

{

instead of returning "response1". How can I get my code to return fields in nested JSON?

  • 写回答

1条回答 默认 最新

  • dongyong9224 2018-07-03 04:55
    关注

    If request is in json form then, you should json_decode it first and then try to access. nested key should be accessed as:

    $nested = json_decode($_POST["nested"], true);
    $field = $nested["field1"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题
  • ¥20 oracle RAC 怎么配置啊,配置
  • ¥15 excel 日常使用中出现问题