从String中删除引号并转换为JSON PHP和Regex

时间:2022-09-15 13:02:42

I know there may be some same questions here but I am not able find this solution so i am writing this and needs to know if there is a possiblity for me to remove quotes from string before curly braces and after closing curly braces like this

我知道这里可能有一些相同的问题但是我找不到这个解决方案所以我写这个并且需要知道我是否有可能在花括号之前从字符串中删除引号并且在关闭像这样的花括号之后

String before

字符串之前

'["{ "zone" : 1, "cat_id" : 1, "subcat" : 1}","{ "zone" 1:, "cat_id" : 1, 
 "subcat" : 2}"]'

String after

字符串之后

'[{ "zone" :1, "cat_id" : 1, "subcat" : 1},{ "zone" :1, "cat_id" : 1, "subcat" :2}]' 

5 个解决方案

#1


1  

simple way

简单的方法

$str = '["{ "zone" : 1, "cat_id" : 1, "subcat" : 1}","{ "zone" 1:, "cat_id" : 1,  "subcat" : 2}"]';


$str = str_replace('["{', "[{", $str);
$str = str_replace('}"]', "}]", $str);

$jsonObject = json_decode($str);

#2


0  

There is a function in PHP called json_encode() which will correctly encode a JSON String from a PHP data structure like an object or an array.

PHP中有一个名为json_encode()的函数,它将从PHP数据结构(如对象或数组)中正确编码JSON字符串。

So first build your data in the data structure you want it in and then run json_encode() on it like this

首先在您想要的数据结构中构建数据,然后像这样在其上运行json_encode()

$arr = array();

$o = new stdClass();
$o->zone = 1;
$o->cat_id = 1;
$o->subcat = 1;

$arr[] = $o;

$o = new stdClass();
$o->zone = 1;
$o->cat_id = 1;
$o->subcat = 2;

$arr[] = $o;

$json_string = json_encode($arr);

echo $json_string;

Result:

结果:

[{"zone":1,"cat_id":1,"subcat":1},{"zone":1,"cat_id":1,"subcat":2}]

#3


0  

Use str_replace to replace the double quotes.

使用str_replace替换双引号。

$str = '["{ "zone" : 1, "cat_id" : 1, "subcat" : 1}","{ "zone" 1:, "cat_id" : 1, "subcat" : 2}"]';
echo $str;
echo "<br>";
$str = str_replace("}\"]", "}]", str_replace('["{', "[{", $str));
echo $str;

#4


0  

You can do it in one pass using the translation function strtr:

您可以使用转换函数strtr一次完成:

$str = strtr($str, [ '"{' => '{', '}"' => '}' ]);

Obviously this approach will fail if one the value contains "{ or }".

显然,如果值包含“{或}”,则此方法将失败。

The best way is to fix the code that produces this wrong JSON.

最好的方法是修复产生这个错误JSON的代码。

#5


0  

I have got it done by this method

我通过这种方法完成了它

$str = '["{ "zone" : 1, "cat_id" : 1, "subcat" : 1}",
         "{ "zone" 1:, "cat_id" : 1,  "subcat" : 2}"]';

$str = str_replace('["{', "[{", $str);
$str = str_replace('}"]', "}]", $str);
$str = str_replace('}",', "},", $str);
$str = str_replace('"{', "{", $str);

This is the way to get the desired result

这是获得所需结果的方法

[{"zone":1,"cat_id":1,"subcat":1},{"zone":1,"cat_id":1,"subcat":2}]

#1


1  

simple way

简单的方法

$str = '["{ "zone" : 1, "cat_id" : 1, "subcat" : 1}","{ "zone" 1:, "cat_id" : 1,  "subcat" : 2}"]';


$str = str_replace('["{', "[{", $str);
$str = str_replace('}"]', "}]", $str);

$jsonObject = json_decode($str);

#2


0  

There is a function in PHP called json_encode() which will correctly encode a JSON String from a PHP data structure like an object or an array.

PHP中有一个名为json_encode()的函数,它将从PHP数据结构(如对象或数组)中正确编码JSON字符串。

So first build your data in the data structure you want it in and then run json_encode() on it like this

首先在您想要的数据结构中构建数据,然后像这样在其上运行json_encode()

$arr = array();

$o = new stdClass();
$o->zone = 1;
$o->cat_id = 1;
$o->subcat = 1;

$arr[] = $o;

$o = new stdClass();
$o->zone = 1;
$o->cat_id = 1;
$o->subcat = 2;

$arr[] = $o;

$json_string = json_encode($arr);

echo $json_string;

Result:

结果:

[{"zone":1,"cat_id":1,"subcat":1},{"zone":1,"cat_id":1,"subcat":2}]

#3


0  

Use str_replace to replace the double quotes.

使用str_replace替换双引号。

$str = '["{ "zone" : 1, "cat_id" : 1, "subcat" : 1}","{ "zone" 1:, "cat_id" : 1, "subcat" : 2}"]';
echo $str;
echo "<br>";
$str = str_replace("}\"]", "}]", str_replace('["{', "[{", $str));
echo $str;

#4


0  

You can do it in one pass using the translation function strtr:

您可以使用转换函数strtr一次完成:

$str = strtr($str, [ '"{' => '{', '}"' => '}' ]);

Obviously this approach will fail if one the value contains "{ or }".

显然,如果值包含“{或}”,则此方法将失败。

The best way is to fix the code that produces this wrong JSON.

最好的方法是修复产生这个错误JSON的代码。

#5


0  

I have got it done by this method

我通过这种方法完成了它

$str = '["{ "zone" : 1, "cat_id" : 1, "subcat" : 1}",
         "{ "zone" 1:, "cat_id" : 1,  "subcat" : 2}"]';

$str = str_replace('["{', "[{", $str);
$str = str_replace('}"]', "}]", $str);
$str = str_replace('}",', "},", $str);
$str = str_replace('"{', "{", $str);

This is the way to get the desired result

这是获得所需结果的方法

[{"zone":1,"cat_id":1,"subcat":1},{"zone":1,"cat_id":1,"subcat":2}]