我遇到一个JSON和PHP json_decode()
的问题。
我们从客户的发布解决方案中收到JSON,虽然它已经验证,但json_decode()
会跳过部分内容。
{
"articles":{
"article":{
"title":"This is the title",
"document":{
"text_article":{
"p":[
"- The first sentence.",
" "
],
"h3":"The first subtitle",
"p":[
"- One sentence.",
"Another sentence.",
"- A quote.",
" "
],
"h3":{
"strong":"Second subtitle"
},
"p":[
"An additional sentence",
"One more.",
{
"a":{
"href":"https://www.example.com",
"target":"_blank",
"$":"Link text"
}
},
"(Some extra information near the bottom)"
]
}
},
"knr":"0001"
}
}
}
导入后,它看起来像这样:
{
"articles": {
"article": {
"title": "This is the title",
"document": {
"text_article": {
"p": [
"An additional sentence",
"One more.",
{
"a": {
"href": "https://www.example.com",
"target": "_blank",
"$": "Link text"
}
},
"(Some extra information near the bottom)"
],
"h3": {
"strong": "Second subtitle"
}
}
},
"knr": "0001"
}
}
}
我怀疑问题是“text_article”中存在多个“p”和“h3”元素。但this online validator按预期显示,因此我们的客户认为它是正确的。 (JSONLint显示与json_decode()
相同的问题)
任何方法都可以将其正确导入PHP,或者我是否正确推动重写代码?
答案 0 :(得分:3)
你不会那样工作。 json_decode将数据导出为php对象或数组 - 因此不允许重复的键/属性。
也许您可以说服客户将json格式更改为以下内容:
{
"articles":{
"article":{
"title":"This is the title",
"document":{
"text_article":[
{
"type":"p",
"content":[
"- The first sentence."
]
},
{
"type":"h3",
"content":[
"The first subtitle."
]
},
{
"type":"p",
"content":[
"- One sentence.",
"Another sentence.",
"- A quote."
]
}
]
},
"knr":"0001"
}
}
}
在那里,您有一个数组text_article
,其中包含每个标记的对象 - 每个标记都包含一个内容数组。可以根据需要通过其他属性扩展对象。
答案 1 :(得分:1)