2011-06-15 95 views
3

我有一个问题,关于从WordPress自定义帖子meta抓取的JSON字符串。WordPress的自定义后元meta json字符串将不会解码与json_decode()

1)我抓住自定义帖子元,并将其保存为$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"}]} 

4)我var_dump解码的字符串。即使字符串相同,WordPress的字符串也不会解码并生成NULL。硬编码的字符串解码很好。

var_dump(json_decode($json1)); 
var_dump(json_decode($json2)); 

结果:

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的字符串有什么问题?


更新:

var_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_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"}]}" 

是什么字符串后的整数意味着什么呢?


更新: 这是hex_dump()结果:

$ 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)); 

感谢您考虑看看。

回答

2

使用回声比较字符串并未真正发挥作用。我建议你将字符串转储为十六进制值。请参阅here for a hex_dump php routine。有了这个,你会看到这些不同之处,可以方便地跟踪编码问题。

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

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

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

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

echo '<pre>', htmlspecialchars($json1), '</pre>'; 
echo '<pre>', htmlspecialchars($json2), '</pre>'; 

如果你这样做你的字符串应该告诉你在哪里得到的JSON根据您添加到您的问题hex_dump()输出断。

+0

我已经用hex_dump更新了原始文章,并且显然有区别。但是'can_be_valid_utf8_statemachine()'对于'$ json1'和'$ json2'均返回true。谢谢你的提示! – Carl 2011-06-15 15:00:58

+0

是的,从'$ json1'的十六进制转储快速扫描它看起来也像UTF-8。 – hakre 2011-06-15 15:02:54

+0

好吧,卡尔,你有字符串内的HTML实体。当您将其输出到浏览器时,您看不到差异,但那些差异在那里。看起来他们破坏了你的json语法。我会更新答案。 – hakre 2011-06-15 15:07:12

1

其可能$json1字符串正在被转义...尝试

var_dump(json_decode(stripslashes($json1)));

+0

这是不是这样,它仍然返回NULL。谢谢你尝试! – Carl 2011-06-15 14:32:06

+0

'var_dump($ json1);'''var_dump($ json2);''是什么内容? – JamesHalsall 2011-06-15 14:37:45

+0

现在我们到了某处: $ json1:'string(282)“{”locations“:[{”number“:1,”header“:”这是一个标题“,”desc“:”This “,”address“:”Infinity Loop Cupertino“,”lat“:”37.3322024“,”lng“:” - 122.030755“}]}”' $ json2:'string(162)“{ “位置”:[{“number”:1,“header”:“这是一个标题”,“desc”:“这是一个描述”,“地址”:“Infinity Loop Cupertino”,“lat”:“ 37.3322024“,”lng“:” - 122.030755“}]}”' – Carl 2011-06-15 14:40:12

0

尝试 str_replace('\\','',$_POST['you-param']);

+0

不知道为什么这得到了downvote。 'json_decode(str_replace('\\','',get_post_meta($ post-> ID,'google_maps_files',true)),true);'为我工作! – dtbaker 2015-07-16 13:11:01

0

如果的stripslashes不为你工作,尝试str_replace('\\','',$meta_value)

如:json_decode(str_replace('\\','',get_post_meta($post->ID, 'google_maps_files', true)),true); worked for me!

相关问题