解析包含JSONArray JSONA的JSONObject的这个字符串

时间:2017-06-09 05:50:59

标签: php json

我想解析包含JSONArray的JSONObject的JSONObject的这个字符串

Array
(
[Assignments] => Array
    (
        [0] => Array
            (
                [ass_name] => Test123
                [class_id] => 2
                [date_assigned] => 2017-08-23
                [done] => false
                [due] => 2017-08-23 13:34:54
                [id] => 10
                [weight] => 65
            )

    )

)

我尝试使用

$json = json_decode($testing,true);
echo $json;

其中$ testing是我的整个字符串但$ json没有回声。

2 个答案:

答案 0 :(得分:0)

检查此示例,

echo =>输出一个或多个以逗号分隔的字符串

print_r =>不仅接受字符串,还接受其他类型,包括数组和对象,将它们格式化为可读

$testing = array("Assignments"=>array("ass_name"=>"Test123","class_id"=>"2","date_assigned"=>"2017-08-23","done"=>"false","due"=>"2017-08-23 13:34:54","id"=>"10","weight"=>"65"));

$json = json_encode($testing); //convert array to json
echo $json;

$json1 = json_decode($json,true); //convert json to array
print_r($json1);

答案 1 :(得分:0)

不要将echo用于打印数组或对象。

$testing = json_encode(
    array(
        'Assignments' => array(
            0 => array(
                'ass_name'      => 'Test123',
                'class_id'      => 2,
                'date_assigned' => '2017-08-23',
                'done'          => false,
                'due'           => '2017-08-23 13:34:54',
                'id'            => 10,
                'weight'        => 65,
            ),
        ),
    )
);

$json = json_decode($testing, true);
echo $json; // prints "Array"
print_r($json); // prints all items in array