Php WordPress自定义post meta JSON字符串赢得';使用json_decode()进行t解码

Php WordPress自定义post meta JSON字符串赢得';使用json_decode()进行t解码,php,string,json,wordpress,decode,Php,String,Json,Wordpress,Decode,我有一个关于从WordPress自定义post meta抓取的JSON字符串的问题 1) 我抓取自定义post meta并将其保存为$json1 $json1 = get_post_meta(get_the_ID(), 'netr_locations_json', true); 2) 我保存了与$json2相同的字符串,这次是硬编码的 $json2 = '{"locations":[{"number":1,"header":"This is a title","desc":"This is a

我有一个关于从WordPress自定义post meta抓取的JSON字符串的问题

1) 我抓取自定义post meta并将其保存为
$json1

$json1 = get_post_meta(get_the_ID(), 'netr_locations_json', true);
2) 我保存了与
$json2
相同的字符串,这次是硬编码的

$json2 = '{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}';
3) 我呼应琴弦。它们是相同的

echo $json1;
echo $json2;
结果:

{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}

{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}
NULL

object(stdClass)#177 (1) { ["locations"]=> array(1) { [0]=> object(stdClass)#173 (6) { ["number"]=> int(1) ["header"]=> string(15) "This is a title" ["desc"]=> string(22) "This is a description." ["address"]=> string(23) "Infinity Loop Cupertino" ["lat"]=> string(10) "37.3322024" ["lng"]=> string(11) "-122.030755" } } } 
4) I
var\u dump
解码字符串。WordPress中的字符串不会解码并生成NULL,即使它是同一个字符串。硬编码字符串可以很好地解码

var_dump(json_decode($json1));
var_dump(json_decode($json2));
结果:

{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}

{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}
NULL

object(stdClass)#177 (1) { ["locations"]=> array(1) { [0]=> object(stdClass)#173 (6) { ["number"]=> int(1) ["header"]=> string(15) "This is a title" ["desc"]=> string(22) "This is a description." ["address"]=> string(23) "Infinity Loop Cupertino" ["lat"]=> string(10) "37.3322024" ["lng"]=> string(11) "-122.030755" } } } 
我错过了什么?WordPress中的字符串有什么问题


更新:

$json1 = get_post_meta(get_the_ID(), 'netr_locations_json', true);
$json2 = '{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}';

echo $json1;
echo '<br/><br/>';
echo $json2;
echo '<br/><br/>';

var_dump(json_decode($json1));
echo '<br/><br/>';
var_dump(json_decode($json2));
var\u dump($json1)
给出了以下信息:

string(282) "{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}" 
var\u dump($json2)
给出了:

string(162) "{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}"
字符串后面的整数是什么意思


更新: 这是十六进制转储()的结果。:

$json1:

00 000 22 7B 26 71 75 6F 74 3B 6C 6F 63 61 74 69 6F 6E "{" location
10 016 73 26 71 75 6F 74 3B 3A 5B 7B 26 71 75 6F 74 3B s": [{"
20 032 6E 75 6D 62 65 72 26 71 75 6F 74 3B 3A 31 2C 26 number&q uot;:1,& 
30 048 71 75 6F 74 3B 68 65 61 64 65 72 26 71 75 6F 74 quot;hea der" 
40 064 3B 3A 26 71 75 6F 74 3B 54 68 69 73 20 69 73 20 ;:" This is 
50 080 61 20 74 69 74 6C 65 26 71 75 6F 74 3B 2C 26 71 a title& quot;,&q 
60 096 75 6F 74 3B 64 65 73 63 26 71 75 6F 74 3B 3A 26 uot;desc ":& 
70 112 71 75 6F 74 3B 54 68 69 73 20 69 73 20 61 20 64 quot;Thi s is a d 
80 128 65 73 63 72 69 70 74 69 6F 6E 2E 26 71 75 6F 74 escripti on." 
90 144 3B 2C 26 71 75 6F 74 3B 61 64 64 72 65 73 73 26 ;," address& 
A0 160 71 75 6F 74 3B 3A 26 71 75 6F 74 3B 49 6E 66 69 quot;:&q uot;Infi 
B0 176 6E 69 74 79 20 4C 6F 6F 70 20 43 75 70 65 72 74 nity Loo p Cupert 
C0 192 69 6E 6F 26 71 75 6F 74 3B 2C 26 71 75 6F 74 3B ino" ;," 
D0 208 6C 61 74 26 71 75 6F 74 3B 3A 26 71 75 6F 74 3B lat" ;:" 
E0 224 33 37 2E 33 33 32 32 30 32 34 26 71 75 6F 74 3B 37.33220 24" 
F0 240 2C 26 71 75 6F 74 3B 6C 6E 67 26 71 75 6F 74 3B ,"l ng" 
100 256 3A 26 71 75 6F 74 3B 2D 31 32 32 2E 30 33 30 37 :"- 122.0307 
110 272 35 35 26 71 75 6F 74 3B 7D 5D 7D 22 55" }]}"
$json2:

00 000 7B 22 6C 6F 63 61 74 69 6F 6E 73 22 3A 5B 7B 22 {"locati ons":[{" 
10 016 6E 75 6D 62 65 72 22 3A 31 2C 22 68 65 61 64 65 number": 1,"heade 
20 032 72 22 3A 22 54 68 69 73 20 69 73 20 61 20 74 69 r":"This is a ti 
30 048 74 6C 65 22 2C 22 64 65 73 63 22 3A 22 54 68 69 tle","de sc":"Thi 
40 064 73 20 69 73 20 61 20 64 65 73 63 72 69 70 74 69 s is a d escripti 
50 080 6F 6E 2E 22 2C 22 61 64 64 72 65 73 73 22 3A 22 on.","ad dress":" 
60 096 49 6E 66 69 6E 69 74 79 20 4C 6F 6F 70 20 43 75 Infinity Loop Cu 
70 112 70 65 72 74 69 6E 6F 22 2C 22 6C 61 74 22 3A 22 pertino" ,"lat":" 
80 128 33 37 2E 33 33 32 32 30 32 34 22 2C 22 6C 6E 67 37.33220 24","lng 
90 144 22 3A 22 2D 31 32 32 2E 30 33 30 37 35 35 22 7D ":"-122. 030755"} 
A0 160 5D 7D ]} 
所以这显然是有区别的


这是完整的代码:

$json1 = get_post_meta(get_the_ID(), 'netr_locations_json', true);
$json2 = '{"locations":[{"number":1,"header":"This is a title","desc":"This is a description.","address":"Infinity Loop Cupertino","lat":"37.3322024","lng":"-122.030755"}]}';

echo $json1;
echo '<br/><br/>';
echo $json2;
echo '<br/><br/>';

var_dump(json_decode($json1));
echo '<br/><br/>';
var_dump(json_decode($json2));
$json1=get_post_meta(get_ID(),'netr_locations_json',true);
$json2='{“位置”:[{“编号”:1,“标题”:“这是一个标题”,“描述”:“这是一个描述”,“地址”:“无限循环库珀蒂诺”,“lat”:“37.3322024”,“lng”:“-122.030755”}]};
echo$json1;
回音“

”; echo$json2; 回音“

”; 变量转储(json解码($json1)); 回音“

”; 变量转储(json解码($json2));

感谢您查看。

可能是正在转义
$json1
字符串。。。试一试


var_dump(json_解码(stripslashes($json1))

使用echo比较字符串实际上不起作用。我建议您也将字符串转储为十六进制值。看见有了这一点,您将看到它们之间的区别,可以方便地跟踪编码问题

仅适用于UTF-8编码数据。我假设传递给字符串的数据不会验证UTF-8,因此无法提取任何值

您必须首先将数据正确编码为UTF-8,然后才能使用它。为此,您必须了解数据的编码

为了验证字符串是否可以是有效的UTF-8数据,我在问题中发布了一些代码。这是一个名为的函数

除了可能的编码问题之外,在比较这两个值时,在浏览器中显示输出时也要小心。以下是一些更“与浏览器兼容”的输出:

echo'',htmlspecialchars($json1),'';
echo'',htmlspecialchars($json2),'';
如果您使用字符串执行此操作,它将根据您添加到问题中的
hex\u dump()
输出显示json在哪里被破坏。

试试看
str\u replace(“\\”、“,$”POST['you-param')

如果stripslashes不适合您,请尝试
str\u replace('\\','$meta\u值)


例如:
json\u解码(str\u替换('\\','',get\u post\u meta($post->ID,'google\u maps\u files',true)),true);为我工作

情况并非如此,它仍然返回NULL。谢谢你的尝试!
var_dump($json1)的内容是什么
变量转储($json2)?现在我们有进展了:$json1:
string(282)“{”位置:“{”数字:1,“标题:“{”这是一个标题”,“描述:“,”地址:““无限循环库珀蒂诺”,“lat:“37.3322024”,“lng:“-122.030755”}}”
$json2:
string(162){”位置:“{”数字:1,“标题:“:”这是一个标题”,“描述:“:”这是一个描述。“,”address:“Infinity Loop Cupertino”,“lat:“37.3322024”,“lng:“-122.030755”}]}”
为了便于阅读,我用结果更新了原始帖子。@JamesHalsall不确定为什么你的答案没有被选为正确答案,因为
stripslashes()
成功了!我想这是因为Carl也有HTML实体在里面。我用十六进制转储更新了原始帖子,显然有区别。但是,
可以是有效的utf8\U statemachine()
$json1
$json2
返回true。感谢您的提示!是的,从对
$json1
十六进制转储的快速扫描来看,它看起来也像UTF-8。好的,Carl,字符串中有HTML实体。当您将其输出到浏览器时,您看不到差异,但它们在那里。看起来它们破坏了oy你的json语法。我会更新答案。非常感谢!我用
html\u entity\u decode()
对字符串进行了解码,现在它可以作为一个符咒使用。好的,所以数据是“html编码的”-不是一个真正的字符集编码问题。很高兴它起到了作用。我不确定为什么会被否决。
json\u decode(str\u replace('\\\','',get\u post\u meta($post->ID,'google_maps_files',true)),true);
为我工作!