2013-07-19 122 views
0

我有一个json字符串,我想解析小对象的数组,我使用解码器,但它不会帮助,为什么这是幸福吗?无法解码json字符串

我已定义的变量为$ cleanforcharacters

$cleanforcharacters = preg_replace('/["{mtwrdayfsasusseto}"]_/', '', $found['newdiscounthours']); 

这是我的输出

discount_org: "{"day":"8:00","time":"12:00","discount":"10","day":"8:00","time":"12:00","discount":"10"}" 

这是所需的输出(对象数组)

discount_org: [ 
{ 
day: 0, 
time: 8, 
discount: 10 
}, 
{ 
day: 0, 
time: 14, 
discount: 10 
}, 

这是怎么我试过

$ arrayOfEmails = json_decode($ cleanforcharacters);

,这就是我现在越来越

discount_org: { 
day: "20", 
time: "12:00", 
discount: "20" 
} 

剩下的就是不出来或者

回答

0

这是因为你已宣布为对象,按键被覆盖的值,而不是设置为新的值: -

你给了: -

discount_org: "{"day":"8:00","time":"12:00","discount":"10","day":"8:00","time":"12:00","discount":"10"}" 

它应该是: -

discount_org: "[{"day":"8:00","time":"12:00","discount":"10"},{"day":"8:00","time":"12:00","discount":"10"}]" 

然后使用: -

$arrayOfEmails = json_decode($cleanforcharacters,true); 

这会给你正确的结果。

+0

嗯,这不是因为它是一个字符串,而不是一个对象吗?如果是这样,我不认为解码器使用字符串 – Zaz