【问题标题】:send jsonobject to server but php can not covert json object to array将 json 对象发送到服务器,但 php 无法将 json 对象转换为数组
【发布时间】:2018-01-09 10:01:21
【问题描述】:

我使用 volley 将 json 对象发送到服务器并在服务器中获取数据,但我无法将 jason 对象转换为 php 中的数组,并且我的代码不起作用

    {
	"type": "get_new_products",
	"city": "abhar",
	"page": 0
}

php代码

<?php
$get_post = file_get_contents('php://input');
$post_data = json_decode($get_post, true);
$content_type = $post_data['type'];
echo $content_type; ?>

【问题讨论】:

  • 您是否检查过您是否收到了数据?以echo $get_post; 为例。
  • 这里没有一个完整的问题
  • 是的,我得到数据 $get_post

标签: php android json android-volley


【解决方案1】:

这可能是由于编码。尝试使用utf8_decode()

$jsonString = '{"type": "get_new_products","city": "abhar","page": 0}';
$decodedJson = utf8_decode($jsonString);

// Second parameter must be true to output an array
$jsonArray = json_decode($decodedJson, true);

// Error handling
if (json_last_error()) {
    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            echo 'No errors';
            break;
        case JSON_ERROR_DEPTH:
            echo 'Maximum stack depth exceeded';
            break;
        case JSON_ERROR_STATE_MISMATCH:
            echo 'Underflow or the modes mismatch';
            break;
        case JSON_ERROR_CTRL_CHAR:
            echo 'Unexpected control character found';
            break;
        case JSON_ERROR_SYNTAX:
            echo 'Syntax error, malformed JSON';
            break;
        case JSON_ERROR_UTF8:
            echo 'Malformed UTF-8 characters, possibly incorrectly encoded';
            break;
        default:
            echo 'Unknown error';
            break;
    }
}

// Output values
echo "The type is: ".$jsonArray['type']."\n";
echo "The city is: ".$jsonArray['city']."\n";

这将输出

The type is: get_new_products
The city is: abhar
The page is: 0

echo "The page is: ".$jsonArray['page']."\n";

错误处理已从 PHP.net 手册中复制。

资源

【讨论】:

  • 那是回声没有错误但没有工作,我可以从 jsonobject 获取内容
  • @itstudent 再看看我的回答。我稍微修改了一下。
  • 这段代码运行没有问题,只是我使用了
    inested of \n
  • @itstudent 如果我的回答回答了您的问题,您可以通过单击答案左侧的按钮将其标记为最佳答案。
  • 我替换 $jsonString = file_get_contents('php://input');并将带有 postMan 的 jsonobject 发送到 php,并且工作正常但不回显“无错误”;
猜你喜欢
  • 1970-01-01
  • 2016-12-15
  • 2014-02-10
  • 2014-11-09
  • 1970-01-01
  • 1970-01-01
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多