Php 使用JSON_解码解析JSON文件
我在使用PHP函数JSON_decode解析JSON文件时遇到问题我目前只看到嵌套JSON数组(BANNER2)中的最后一个对象。。我意识到我在JSON文件中使用了重复的键,但我不知道如何以不同的方式构造JSON文件 我的JSON文件:Php 使用JSON_解码解析JSON文件,php,json,Php,Json,我在使用PHP函数JSON_decode解析JSON文件时遇到问题我目前只看到嵌套JSON数组(BANNER2)中的最后一个对象。。我意识到我在JSON文件中使用了重复的键,但我不知道如何以不同的方式构造JSON文件 我的JSON文件: { "project_filename":"testzip", "data":[ { "title":"Quebec", "displayTag":"H1",
{
"project_filename":"testzip",
"data":[
{
"title":"Quebec",
"displayTag":"H1",
"css":"",
"type":"header",
"display_title":"",
"data":[
{
"title":"BANNER1",
"displayTag":"h2",
"css":"hidden",
"type":"collapse",
"display_title":"",
"data":[
{
"title":"160x600",
"displayTag":"p",
"display_title":"DESCRIPTION",
"filename":"300x250",
"type":"banner",
"source":"pages/300x250/index.html",
"width":"300",
"height":"250",
"controls":true
}
],
"title":"BANNER2",
"displayTag":"h2",
"css":"hidden",
"type":"collapse",
"display_title":"",
"data":[
{
"title":"160x600",
"displayTag":"p",
"display_title":"DESCRIPTION",
"filename":"300x250",
"type":"banner",
"source":"pages/300x250/index.html",
"width":"300",
"height":"250",
"controls":true
}
]
}
]
}
]
}
我的PHP文件:
$JSONdata = json_decode($data, true);
foreach ($JSONdata['data'] as $key => $dt) {
foreach ($dt['data'] as $data) {
// use the JSON values in the $dt variable and do stuff with it
这两个横幅应该是阵列中的独立对象,而不是单个对象中的重复键。对象键必须是唯一的
{
"project_filename":"testzip",
"data":[
{
"title":"Quebec",
"displayTag":"H1",
"css":"",
"type":"header",
"display_title":"",
"data":[
{
"title":"BANNER1",
"displayTag":"h2",
"css":"hidden",
"type":"collapse",
"display_title":"",
"data":[
{
"title":"160x600",
"displayTag":"p",
"display_title":"DESCRIPTION",
"filename":"300x250",
"type":"banner",
"source":"pages/300x250/index.html",
"width":"300",
"height":"250",
"controls":true
}
]
},{
"title":"BANNER2",
"displayTag":"h2",
"css":"hidden",
"type":"collapse",
"display_title":"",
"data":[
{
"title":"160x600",
"displayTag":"p",
"display_title":"DESCRIPTION",
"filename":"300x250",
"type":"banner",
"source":"pages/300x250/index.html",
"width":"300",
"height":"250",
"controls":true
}
]
}
]
}
]
}
问题当然在于这个JSON是如何构造的。虽然在结构上还可以,但从技术上来说,它甚至是无效的。看起来至少和嵌套有冲突。我意识到我在JSON文件中使用了重复的键-如果你有重复的键,那么你得到的不是有效的JSON。项目应该在一个数组中。我在第26行得到了
语法错误:重复键“title”
您应该有一个对象数组,而不是重复键。您首先是如何创建这个无效的JSON的?没有JSON编码库可以做到这一点。我的想法是需要第一个数组来包含region->banner number->size。因此,每个地区都需要自己的一组条幅编号和这些条幅编号中的尺寸。(很抱歉解释得很混乱)真不敢相信我没看到。非常感谢。