[Solved] send jsonobject to server but php can not covert json object to array


It could be due to the encoding. Try using 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";

This will output

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

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

The error handling has been copied from the PHP.net manual.

Resources

5

solved send jsonobject to server but php can not covert json object to array