PHP json_decode skips part of source JSON

后端 未结 2 1083
失恋的感觉
失恋的感觉 2021-01-22 12:39

I have a problem with a piece of JSON and PHP\'s json_decode(). We receive JSON from a customer\'s publishing solution, and although it validates, json_decod

相关标签:
2条回答
  • 2021-01-22 12:55

    You will not get that to work. json_decode exports the data as php objects or arrays - and therefore no duplicate keys / properties are allowed.

    Maybe you can convince the customer to change the json format into something like this:

    {
        "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"
            }
        }
    }
    

    There, you have an array text_article containing objects for each tag - each containing an array of contents. The objects can be extended by further attributes as needed.

    0 讨论(0)
  • 2021-01-22 13:01

    just to add a possible option...

    the library jsonlint can be used to validate your json string. It will take arguments to either ignore/detect duplicate keys.

    whilst this wont fix your problem, at least youd be able to detect any errors so avoiding data corruption.

    0 讨论(0)
提交回复
热议问题