【问题标题】:Why does json_encode/decode configure my array this way? PHP为什么 json_encode/decode 会以这种方式配置我的数组? PHP
【发布时间】:2012-07-28 18:40:51
【问题描述】:

我正在尝试发送一个 JSON 文件来进行一些测试。我有一个创建多维数组的简单测试文件。这是测试文件:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

$user['Mongo'] = null; 
$user['Facebook'] = 12345;
$user['Twitter'] = null;
$user['Foursquare'] = null;
$user['Google'] = null;
$user['Name'] = "Bill Gates";
$user['Sex'] = 'M';
$user['Age'] = 26;
$user['Birthday'] = "1985-08-13";
$user['Friends'][0]['Mongo'] = null;
$user['Friends'][0]['Facebook'] = 123456;
$user['Friends'][0]['Twitter'] = null;
$user['Friends'][0]['Foursquare'] = null;
$user['Friends'][0]['Google'] = null;
$user['Friends'][0]['Name'] = "John Smith";
$user['Friends'][0]['Relationship'] = "Open";
$user['Friends'][1]['Mongo'] = null;
$user['Friends'][1]['Facebook'] = 1234567;
$user['Friends'][1]['Twitter'] = null;
$user['Friends'][1]['Foursquare'] = null;
$user['Friends'][1]['Google'] = null;
$user['Friends'][1]['Name'] = "Martina McBride";
$user['Friends'][1]['Relationship'] = "Open";

$user_json = json_encode($user);

$call = curl_init('http://MY_IP_HERE/user_login.php');

curl_setopt($call, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($call, CURLOPT_POSTFIELDS, $user_json);
curl_setopt($call, CURLOPT_RETURNTRANSFER, true);
curl_setopt($call, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: '.strlen($user_json)));

$result = curl_exec($call);
curl_close($call);

echo $result;
?>

我正在尝试像这样检索文件:

<?php
include_once('interaction_class.php');

error_reporting(E_ALL);
ini_set('display_errors', '1');

$fp = fopen('php://input', 'r');
$rawData = stream_get_contents($fp);

$user = json_decode($rawData);

if ($user['Mongo'] == null)
{
    $user_id = $interaction->new_guest($user);
}
//...Other stuff...

我在 if 语句行中收到有关 stdCLass 类对象的错误。所以,我做了一个 var_dump(),结果如下:

object(stdClass)#1 (10) 
{ 
    ["Mongo"] => NULL 
    ["Facebook"] => int(12345) 
    ["Twitter"] => NULL 
    ["Foursquare"] => NULL 
    ["Google"] => NULL 
    ["Name"] => string(15) "Bill Gates" 
    ["Sex"] => string(1) "M" 
    ["Age"] => int(26) 
    ["Birthday"] => string(10) "1985-08-13" 
    ["Friends"] => array(2) 
    { 
        [0] => object(stdClass)#2 (7) 
        { 
            ["Mongo"] => NULL 
            ["Facebook"] => int(123456)
            ["Twitter"] => NULL 
            ["Foursquare"] => NULL 
            ["Google"] => NULL 
            ["Name"] => string(10) "John Smith" 
            ["Relationship"] => string(4) "Open" 
        } 
        [1] => object(stdClass)#3 (7) 
        { 
            ["Mongo"] => NULL 
            ["Facebook"] => int(1234567) 
            ["Twitter"] => NULL 
            ["Foursquare"] => NULL 
            ["Google"] => NULL 
            ["Name"] => string(15) "Martina McBride" 
            ["Relationship"] => string(4) "Open" 
        } 
    } 
}

我的问题是,为什么我像这样执行 json_decode() 后无法访问信息:

$thing['Key']

为什么要把它解码成一个对象而不是一个数组?

提前感谢您的帮助!

【问题讨论】:

    标签: php post json


    【解决方案1】:

    PHP函数json_decode()默认返回对象:http://www.php.net/manual/en/function.json-decode.php

    如果您想使用数组,请将布尔值“true”添加为函数中的可选第二个参数,如下所示:

    $user = json_decode($rawData, true);
    

    这会将数组返回到变量$user

    如果你想继续加载对象,你可以使用$user['Mongo']而不是$user-&gt;Mongo

    希望这会有所帮助,祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2014-08-28
      • 1970-01-01
      • 2012-05-11
      • 2020-06-20
      • 2012-11-09
      • 2014-03-21
      相关资源
      最近更新 更多