【发布时间】:2016-09-02 23:58:57
【问题描述】:
我有一个像这样的无效 json 字符串:
{
"cat":{
"id":"1234567",
"source_id":null,
"title_en":"first season",
"description_en":"This is spring category "
},
"videos":[
{
"id":"312412343",
"url":"\/2015-07-17\/1abcd.mp4",
"img":"image\/44\/\/2015-07-17\/1abcd.jpg",
"description1":"some text for" description here",
"description2":"some text for" description here2",
"title":"first title here"
},
{
"id":"2342343",
"url":"\/2015-07-16\/2dcdeg.mp4",
"img":"images\/44\/\/2015-07-16\/2dcdeg.jpg",
"description1":"some other text here" for description ",
"description2":"some other text here" for description2 ",
"title":"second title here"
}
]
}
我想使用正则表达式将 description1 和 description2 的值更改为空字。任何人都可以帮助我如何使用正则表达式来实现这一点吗?谢谢。
"description1":"empty",
"description2":"empty",
【问题讨论】:
-
让它变得简单。解码您的 json,更改数组值,然后再次对其进行编码。
-
感谢您的回复。我的 JSON 并非一直有效(有时它在 description1 和 description2 中有双引号)。如果您建议的方法适用于无效的 JSON,我不知道,所以我会更喜欢正则表达式。
-
那你解决了错误的问题。如果你期望 json 你应该得到有效的 json,否则你做错了。如果您需要使用正则表达式来解决它,它不会使问题变得更好。
-
我使用的 api 给了我无效的 json,所以我不能做任何事情,除非我自己修复 json,以某种方式使 descrption2 和 description 1 为空!但不知道怎么做!
-
那么 API 就是垃圾。但请确保在您的问题中包含您有时有 invliad json 的信息。还可以看看regex101.com 来玩并构造一个正则表达式:)
标签: php json regex preg-replace preg-match-all