我是javascript的新手,请你帮我转换为JSON格式到普通数组
{"data": [
{"name": "Bhengu","surname":"Nathi"},
{"name": "Tsunami","surname":"Msipha"},
{ "name": "Fish","surname":"Lee"}
]};
data= [
{name: Bhengu, surname: Nathi},
{name: Tsunami, surname: Msipha},
{ name: Fish, surname: Lee}
];
答案 0 :(得分:2)
你可以使用php的json_decode
功能
$json = '{"data": [{ "name": "Bhengu","surname":"Nathi"}, { "name": "Tsunami","surname":"Msipha"}, { "name": "Fish","surname":"Lee"} ]}';
$data = json_decode($json);
var_dump($data);
输出将是:
object(stdClass)[1]
public 'data' =>
array
0 =>
object(stdClass)[2]
public 'name' => string 'Bhengu' (length=6)
public 'surname' => string 'Nathi' (length=5)
1 =>
object(stdClass)[3]
public 'name' => string 'Tsunami' (length=7)
public 'surname' => string 'Msipha' (length=6)
2 =>
object(stdClass)[4]
public 'name' => string 'Fish' (length=4)
public 'surname' => string 'Lee' (length=3)
答案 1 :(得分:1)
{"data": [{ "name": "Bhengu","surname":"Nathi"}, { "name": "Tsunami","surname":"Msipha"}, { "name": "Fish","surname":"Lee"} ]};
heredata= [ { name: Bhengu, surname: Nathi}, { name: Tsunami, surname: Msipha},{ name: Fish, surname: Lee} ];
您可以使用Json2 script
然后你可以运行:var myObject = JSON.parse(myJSONtext, reviver);
应该为你提供你需要的数组作为myObject
答案 2 :(得分:0)
将json_decode()
与第二个参数true
一起使用,这将强制将返回的对象转换为数组。
$result = json_decode($jsonStr, true);
if (isset($result)) {
do something here...
}
检查manual条目是否详细使用(例如$depth
参数)。