我有JSON值,存储在php变量$ string
中 $string= '{
"SUCCESS": [
{
"MESSAGE": "IMEI Service List",
"LIST": {
"Cable": "This is cable1",
"Cable servers and log": {
"GROUPNAME": "Cable servers and log",
"SERVICES": {
"110": {
"SERVICEID": 995,
"TIME": " Minutes",
"Requires.MEP": "None",
"Requires.PRD": "None"
}
}
}
}
}
]
}';
我在php中访问这些JSON值时出现问题
对于php代码我使用了这段代码:
$json_a=json_decode($string,true);
foreach($json_a[SUCCESS] as $p)
{
echo 'Message: '.$p[MESSAGE]. '</br>';
}
上面的PHP代码很好但我不知道如何显示的值 php中的“GROUPNAME”和“SERVICEID”
答案 0 :(得分:0)
$data=json_decode($string,true);
foreach ($data["SUCCESS"] as $loopdata)
{
//spaces occur in json key cable servers and log
echo $loopdata["LIST"]["Cable servers and log"]["GROUPNAME"];
}
注意:您的json无效。以下是有效的
$string={
"SUCCESS": [
{
"MESSAGE": "IMEI Service List",
"LIST": {
"Cable": "This is cable1",
"Cable servers and log": {
"GROUPNAME": "Cable servers and log",
"SERVICES": {
"110": {
"SERVICEID": 995,
"TIME": " Minutes",
"Requires.MEP": "None",
"Requires.PRD": "None"
}
}
}
}
}
]
}
答案 1 :(得分:0)
首先引用带有' or "
['text']
良好做法的assos数组键,有时键有空格,例如[Cable servers and log]
,然后没有引号致命错误
解析错误:语法错误,意外的&#39;服务器&#39; (T_STRING),期待&#39;]&#39;
你可以通过这种方式获得&#34; GROUPNAME&#34; 尝试此代码
foreach($json_a['SUCCESS'] as $p)
{
echo 'Message: '.$p['MESSAGE']. '</br>';
echo 'GROUPNAME: '.$p['LIST']['Cable servers and log']['GROUPNAME']. '</br>';
}