Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP json_解码跳过部分源json_Php_Json_Import - Fatal编程技术网

PHP json_解码跳过部分源json

PHP json_解码跳过部分源json,php,json,import,Php,Json,Import,我对一段JSON和PHP的JSON\u decode()有问题。 我们从客户的发布解决方案接收JSON,尽管它进行了验证,JSON\u decode()跳过了部分内容 { "articles":{ "article":{ "title":"This is the title", "document":{ "text_article":{ "p":[ "- The first sentence.",

我对一段JSON和PHP的
JSON\u decode()
有问题。 我们从客户的发布解决方案接收JSON,尽管它进行了验证,
JSON\u 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"
    }
  }
}
{
    "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”中存在几个“p”和“h3”元素。但它按预期显示,因此我们的客户认为它是正确的。(但显示的问题与
json\u decode()
相同)


有什么方法可以让它正确地导入到PHP,或者我要求重写代码是正确的吗?

您将无法让它正常工作。json_decode将数据导出为php对象或数组,因此不允许重复的键/属性

也许您可以说服客户将json格式更改为以下格式:

{
  "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"
    }
  }
}
{
    "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\u article
包含每个标记的对象,每个标记包含一个内容数组。这些对象可以根据需要通过进一步的属性进行扩展。

只需添加一个可能的选项

该库可用于验证json字符串。它需要参数来忽略/检测重复的密钥


虽然这不会解决您的问题,但至少您能够检测到任何错误,从而避免数据损坏。

这是PHP中的一个限制,而在其他编程语言中可能没有?在这种情况下,客户可能是正确的,我需要寻找一种中间解决方案。我不知道有哪种编程语言没有这种限制。我非常确信,所使用的开发语言不应该依赖于客户提供的数据格式。至少,您可以编写某种解析器,在使用
json\u decode
之前更改json文件的结构。看起来最好的解决方案是按照您的建议,我将尝试让客户将json更改为使用数组。