我如何解析json数组字符串数组对php


How can I parse json array string to array on php

如何将json数组字符串解析为php上的数组

'[{"a": "1", "b": "2"}, {"a": "3"}]'

似乎json_decode只允许解析对象而不允许解析数组。在使用json_decode之前,应该手动解析到数组吗?


字符串似乎有问题。我用json得到一个变量,如果我输出它,json看起来是有效的

echo($jsonvar); //result [{"title":"Home","id":"/","url":"/"}]

但是当我尝试从变量中解析字符串时,结果是什么也没有,即使字符串被修剪

echo('[{"title":"Home","id":"/","url":"/"}]', true); //nice parsed array
echo($jsonvar, true); //nothing
echo(trim($jsonvar, " 't'n'r'0'x0B"), true); //nothing

true作为第二个参数传递给json_decode()以解析json字符串到数组

$json='[{"a": "1", "b": "2"}, {"a": "3"}]';
$arr= json_decode($json,true); 
print_r($arr);

您可以使用 json_decode()

中的true标志将json放入数组中
<?php
$str = '[{"a": "1", "b": "2"}, {"a": "3"}]';
$arr=json_decode($str,true);
print_r($arr);