PHP在json字符串中的变量前后添加双引号

时间:2022-09-15 13:41:48

I'm trying to add double quotes to Every Variable like file: and label: in a json string. For example:

我正在尝试为每个变量添加双引号,如file:和label:在json字符串中。例如:

{file:"File_Name.mp3"},{file:"File_Name.mp4",label:"720"},{file:"File_Name.mp4",label:"360"}

Should be:

{"file":"File_Name.mp3"},{"file":"File_Name.mp4","label":"720"},{"file":"File_Name.mp4","label":"360"}

How can i do this? I read a article of * There but my problem not solved with this. I'm supposed to use regular expressions. Unfortunately, but i am new.

我怎样才能做到这一点?我读了一篇*文章,但我的问题没有解决。我应该使用正则表达式。不幸的是,但我是新人。

and i need to get file name if label is 360 so what will be the php code give the filename as i point-out in php.

如果标签是360,我需要获取文件名,那么php代码将给出文件名,因为我在php中指出了。

Thanks.

1 个解决方案

#1


0  

OK, if you're new to all this, maybe regular expressions aren't the best answer.

好吧,如果你对这一切都不熟悉,也许正则表达式不是最好的答案。

You can also use a simple str_replace function (doc) like this :

您还可以使用这样的简单str_replace函数(doc):

str_replace(array('file:', 'label:'),array('"file":', '"label":'),$your_json_string);

str_replace(array('file:','label:'),array('“file”:','“label”:'),$ your_json_string);

To exploit the json in php, you'll need to decode it using json_decode, and use a loop to check the label values (I advise a foreach loop).

要在php中利用json,你需要使用json_decode对其进行解码,并使用循环来检查标签值(我建议使用foreach循环)。

#1


0  

OK, if you're new to all this, maybe regular expressions aren't the best answer.

好吧,如果你对这一切都不熟悉,也许正则表达式不是最好的答案。

You can also use a simple str_replace function (doc) like this :

您还可以使用这样的简单str_replace函数(doc):

str_replace(array('file:', 'label:'),array('"file":', '"label":'),$your_json_string);

str_replace(array('file:','label:'),array('“file”:','“label”:'),$ your_json_string);

To exploit the json in php, you'll need to decode it using json_decode, and use a loop to check the label values (I advise a foreach loop).

要在php中利用json,你需要使用json_decode对其进行解码,并使用循环来检查标签值(我建议使用foreach循环)。