我有以下JSON字符串
[{
"email": "first@gmail.com",
"createdate": "2016-03-24 08:16:52",
"from_email": null,
"from_liked_pageid": null,
"source": "direct",
"bonus_entry_url": "\/qbxttt\/h1ryd2",
"entry_url": "https:\/\/admin.woobox.com\/manage\/offers\/entries\/20160909-1\/nLbCpFW0ojJaSNdgFcfMqkCvmjeSUbirdYguz%2F2Ya4O5xalcyrAvKb7MCR71frLSepDTRVcYBMkJ6C7tqotSzQ%3D%3D",
"shareurl": "http:\/\/woobox.com\/qbxttt\/h1ryd2",
"picked": "0",
"actionbutton": "",
"custom_3_first": "first",
"custom_3_last": "first",
"custom_4": "7176585018",
"fullname": "first first",
"id": "qbxttt"
}, {
"email": "second@gmail.com",
"createdate": "2016-03-24 08:17:01",
"from_email": null,
"from_liked_pageid": null,
"source": "direct",
"bonus_entry_url": "\/qbxttt\/h1rygc",
"entry_url": "https:\/\/admin.woobox.com\/manage\/offers\/entries\/20160909-1\/Tmg5lBCuKn87OCSHUBHvbcNHX610SbdOoAuHVqkJI1GpszeBwpL9%2BFrQf06SD8S25LzQq0uy21EFyZyjglPM2A%3D%3D",
"shareurl": "http:\/\/woobox.com\/qbxttt\/h1rygc",
"picked": "0",
"actionbutton": "",
"custom_3_first": "second",
"custom_3_last": "second",
"custom_4": "7176585011",
"fullname": "second second",
"id": "qbxttt"
}, {
"email": "third@gmail.com",
"createdate": "2016-03-24 08:17:33",
"from_email": null,
"from_liked_pageid": null,
"source": "direct",
"bonus_entry_url": "\/qbxttt\/h1ryin",
"entry_url": "https:\/\/admin.woobox.com\/manage\/offers\/entries\/20160909-1\/kD5srK7WdnOcu2EAw7DEOc8VE7rVRMDgkJc8UQqxnzh9oqDBm%2BNb%2Bp9%2BlpcvXPO6hy9Srg3SXyvZM%2FaXpMA1Uw%3D%3D",
"shareurl": "http:\/\/woobox.com\/qbxttt\/h1ryin",
"picked": "0",
"actionbutton": "",
"custom_3_first": "third",
"custom_3_last": "third",
"custom_4": "7176585104",
"fullname": "third third",
"id": "qbxttt"
}][{
"email": "conference.room@gmail.com",
"createdate": "2016-03-24 08:29:10",
"from_email": null,
"from_liked_pageid": null,
"source": "direct",
"bonus_entry_url": "\/qbxttt\/h1s15g",
"entry_url": "https:\/\/admin.woobox.com\/manage\/offers\/entries\/20160909-1\/V5%2FvZ8XE197qahQoJVi%2FAha5toMAAz19mAl%2FowX6NsoqGmDMJiV3adHQfPcJrU3WxQ6NagWZ8xh%2FZexXCEMyBg%3D%3D",
"shareurl": "http:\/\/woobox.com\/qbxttt\/h1s15g",
"picked": "0",
"actionbutton": "",
"custom_3_first": "conference",
"custom_3_last": "conference",
"custom_4": "9195583344",
"fullname": "conference conference",
"id": "qbxttt"
}, {
"email": "sally.huang@gmail.com",
"createdate": "2016-03-24 08:31:48",
"from_email": null,
"from_liked_pageid": null,
"source": "direct",
"bonus_entry_url": "\/qbxttt\/h1s1xa",
"entry_url": "https:\/\/admin.woobox.com\/manage\/offers\/entries\/20160909-1\/Fex6pkK3YFSBT%2F%2FD37bvV1ik5Gum3tDOMuYsQwNz%2FVWjNTyLMd%2BJI3E%2FZnUcrshAIhE62UbCHPwoncQ5NYPYUg%3D%3D",
"shareurl": "http:\/\/woobox.com\/qbxttt\/h1s1xa",
"picked": "0",
"actionbutton": "",
"custom_3_first": "sally",
"custom_3_last": "huang",
"custom_4": "2153342222",
"fullname": "sally huang",
"id": "qbxttt"
}][{
"email": "test.gmail@gmail.com",
"createdate": "2016-03-24 08:36:01",
"from_email": null,
"from_liked_pageid": null,
"source": "direct",
"bonus_entry_url": "\/qbxttt\/h1s2ql",
"entry_url": "https:\/\/admin.woobox.com\/manage\/offers\/entries\/20160909-1\/bop12HUD0vcV339mAB4RkEg%2BiYj6oDB%2B2NDK7auwwPLdqzWx8Jh4ppj%2BhRt2VDOpE1hrZ7y0yjj65qLdynoCwg%3D%3D",
"shareurl": "http:\/\/woobox.com\/qbxttt\/h1s2ql",
"picked": "0",
"actionbutton": "",
"custom_3_first": "test",
"custom_3_last": "gmail",
"custom_4": "2152931118",
"fullname": "test gmail",
"id": "qbxttt"
}][{
"email": "sixth.test@mgmgrand.com",
"createdate": "2016-03-24 09:02:07",
"from_email": null,
"from_liked_pageid": null,
"source": "direct",
"bonus_entry_url": "\/qbxttt\/h1s8da",
"entry_url": "https:\/\/admin.woobox.com\/manage\/offers\/entries\/20160909-1\/olKS0uHVdNb%2Fv9EkNHiXGlPVZVgVZ1r9UqmrHZYxLi33muihmcf3NkjMC59lh6jkM%2F9C5moj4t%2BupGHwtF384w%3D%3D",
"shareurl": "http:\/\/woobox.com\/qbxttt\/h1s8da",
"picked": "0",
"actionbutton": "",
"custom_3_first": "sixth",
"custom_3_last": "sixth",
"custom_4": "2151112333",
"fullname": "sixth sixth",
"id": "qbxttt"
}]
JSON字符串是从.txt文件解码的,遗憾的是,根据数据发布到.txt文件的时间,它会创建一个新的括号][
,导致整个字符串无效。
我试图不是从字符串的开头或结尾替换括号][
,而是在中间发生它的任何时候。请参阅第41,89,97行。有关如何更换字符串的任何想法?
$url = 'http://www.mywebsite.com/sweeps/test.txt';
$content = file_get_contents($url);
$tmp = explode('][', $content);
foreach ($tmp as $json_part) {
$user = json_decode('['.rtrim(ltrim($json_string, '['), ']').']', true);
$result = $wrap->add(array(
'EmailAddress' => $user->email,
'Name' => $user->fullname,
'Resubscribe' => true
));
}
答案 0 :(得分:0)
只需替换:
$user = json_decode('['.rtrim(ltrim($json_string, '['), ']').']', true);
带
$json_string = str_replace('][', ',', $json_string);
$user = json_decode($json_string);