Javascript 如何解析这种json数组
我有一个json数组,如:Javascript 如何解析这种json数组,javascript,php,arrays,json,web,Javascript,Php,Arrays,Json,Web,我有一个json数组,如: { "0": { "kind": "mammal", "name": "Pussy the Cat", "weight": "12kg", "age": "5" }, "1": { "kind": "mammal", "name": "Roxy the Dog", "weight": "25kg", "age"
{
"0": {
"kind": "mammal",
"name": "Pussy the Cat",
"weight": "12kg",
"age": "5"
},
"1": {
"kind": "mammal",
"name": "Roxy the Dog",
"weight": "25kg",
"age": "8"
},
"2": {
"kind": "fish",
"name": "Piranha the Fish",
"weight": "1kg",
"age": "1"
},
"3": {
"kind": "bird",
"name": "Einstein the Parrot",
"weight": "0.5kg",
"age": "4"
}
}
我删除了一个元素“鱼食人鱼”(UNSET),json代码改为blow:
{
"0": {
"kind": "mammal",
"name": "Pussy the Cat",
"weight": "12kg",
"age": "5"
},
"1": {
"kind": "mammal",
"name": "Roxy the Dog",
"weight": "25kg",
"age": "8"
},
"3": {
"kind": "bird",
"name": "Einstein the Parrot",
"weight": "0.5kg",
"age": "4"
}
}
当我移除元素时,元素的计数器变为0,1,3
所以现在我无法在json元素中拥有完全访问权限
我需要此代码的回显值:
for ($i = 0; $i < $JsonCount - 1; $i++) {
echo "Name :";
echo $json_cod[$i]['name'];
echo "<br/>";
echo "Age :";
echo $json_cod[$i]['age'];
echo "<br/>";
}
尝试使用
foreach
foreach ($json_cod as $item) {
echo "Name :";echo $item['name'];echo "<br/>";
echo "Age :";echo $item['age'];echo "<br/>";
}
foreach($json\u cod作为$item){
echo“名称:”;echo$item['Name'];echo“
”;
echo“年龄:;echo$item['Age'];echo“
”;
}
如果要使用代码回送,从数组中删除值后,请使用$newarray=array\u values($oldarray)
重新为数组编制索引。这是因为unset()
从数组中删除值和键/索引,并保持原样,即它不会重新排列键/索引
如果要重新排列键,请使用array\u slice()。你会得到你想要的。你会做到的
for($i=0; $i <$JsonCount-1 ; $i++)
{
echo "Name :";echo $json_cod[$i]['name'];echo "<br/>";
echo "Age :";echo $json_cod[$i]['age'];echo "<br/>";
}
for($i=0;$i)您的数据结构看起来不像JSON数组。您确定要解析的是JSON数组吗?使用foreach
可以忽略索引,在将数组编码为JSON之前,可以在PHP中重新索引该数组;或者使用
for($i=0; $i <$JsonCount-1 ; $i++)
{
echo "Name :";echo $json_cod[$i]['name'];echo "<br/>";
echo "Age :";echo $json_cod[$i]['age'];echo "<br/>";
}