PHP json_decode 跳过部分源代码 JSON
PHP json_decode skips part of source JSON
我对 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 displays it as intended, so our customer is under the impression that it is correct. (JSONLint 显示了与 json_decode()
相同的问题)
有什么方法可以将其正确导入 PHP,或者我推动重写代码是否正确?
你不会让它工作的。 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
,其中包含每个标签的对象 - 每个标签都包含一个内容数组。可以根据需要通过更多属性扩展对象。
只是为了添加一个可能的选项...
库 jsonlint 可用于验证您的 json 字符串。它将接受 ignore/detect 个重复键的参数。
虽然这不会解决您的问题,但至少您可以检测到任何错误从而避免数据损坏。
我对 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 displays it as intended, so our customer is under the impression that it is correct. (JSONLint 显示了与 json_decode()
相同的问题)
有什么方法可以将其正确导入 PHP,或者我推动重写代码是否正确?
你不会让它工作的。 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
,其中包含每个标签的对象 - 每个标签都包含一个内容数组。可以根据需要通过更多属性扩展对象。
只是为了添加一个可能的选项...
库 jsonlint 可用于验证您的 json 字符串。它将接受 ignore/detect 个重复键的参数。
虽然这不会解决您的问题,但至少您可以检测到任何错误从而避免数据损坏。