javascript - Ajax 回调返回了无效的 json

标签 javascript php ajax json

我尝试了下面的代码,但最终 json 无效。

我的PHP

$myArray = array();
$myArray["item"]["content"] = $item_content;
echo json_encode($myArray);

我的 JavaScript

..
dataType: "JSON",
success: function(data){
console.log(data);
}

所以console.log中的输出是

{
    "item": {
        "coverImg": "<div class=\\\"coverImg\\\" style=\\\"background:url('https:\/\/www-techinasia.netdna-ssl.com\/wp-content\/uploads\/2015\/01\/broken-undersea-cable-brings-internet-misery-to-Vietnam-yet-again.png') no-repeat\\\"><\/div>",
        "title": "Here we go again: broken undersea cable brings internet misery to Vietnam",
        "desc": "The cable operator is locating the breaks and has not yet indicated when this outage \u2013 the latest of several in the past couple of years \u2013 will be fixed.",
        "link": "https:\/\/www.techinasia.com\/broken-undersea-cable-vietnam-internet-again-and-again-and-again\/",
        "content": "<p><img src=\"https:\/\/www-techinasia.netdna-ssl.com\/wp-content\/uploads\/2015\/01\/broken-undersea-cable-brings-internet-misery-to-Vietnam-yet-again.png\" alt=\"broken undersea cable brings internet misery to Vietnam yet again\" width=\"750\" height=\"475\" class=\"aligncenter size-full wp-image-214007\" \/><\/p>\n<p>If you&#8217;re reading this in Vietnam, it likely took several minutes &#8211; enlivened by swearing and gesticulating &#8211; to open this page. That&#8217;s because a Vietnamese segment of the crucial<br \/>\nTrans-Pacific internet cable has reportedly ruptured.<\/p>\n<p>The breakage has occurred on several sections of the S1H segment of the undersea cable that branches off towards Hong Kong, <a href=\"http:\/\/www.thanhniennews.com\/tech\/transpacific-cable-snaps-slowing-down-internet-in-vietnam-37184.html\">reports Thanh Nien News<\/a> citing a statement from cable operator Asia-America Gateway (AAG). This appears to have left other countries in the region, such as Thailand and Malaysia, unaffected as they&#8217;re not on the same branch of the cable.<\/p>\n<p>AAG is locating the breaks and has not yet indicated when this outage &#8211; the latest of several in the past couple of years &#8211; will be fixed. The <a href=\"http:\/\/en.wikipedia.org\/wiki\/Asia-America_Gateway\">20,000-kilometer-long<\/a> series of tubes connects Southeast Asia with the US west coast, going through Guam and Hawaii.<\/p>\n<div id=\"attachment_214005\" style=\"width: 578px\" class=\"wp-caption aligncenter\"><img src=\"https:\/\/www-techinasia.netdna-ssl.com\/wp-content\/uploads\/2015\/01\/Here-we-go-again-broken-undersea-cable-brings-internet-misery-to-Vietnam.gif\" alt=\"Here we go again - broken undersea cable brings internet misery to Vietnam\" width=\"568\" height=\"207\" class=\"size-full wp-image-214005\" \/><p class=\"wp-caption-text\">This GIF from Asia-America Gateway shows the cable&#8217;s route.<\/p><\/div>\n<p>The most recent <a href=\"https:\/\/www.techinasia.com\/vietnam-internet-cable-cut-yet-again\/\">cable breakage occurred a few months ago<\/a>, affecting Malaysia, Singapore, Thailand, Brunei, Vietnam, Hong Kong, Guam, and the Philippines. That took about <a href=\"https:\/\/www.techinasia.com\/southeast-asia-slow-internet-misery-broken-undersea-cable-needs-20-days-to-repair\/\">20 days to fix<\/a> once the issue had been found, amounting to nearly a month of internet woes for people in the affected countries.<\/p>\n<p><center><\/p>\n<blockquote class=\"twitter-tweet\" lang=\"en\"><p>Fnng. Groan. Damn internet. Back to dialup speed again. <a href=\"http:\/\/t.co\/LWvb2JVSRj\">http:\/\/t.co\/LWvb2JVSRj<\/a><\/p>\n<p>&mdash; josh kope\u010dek (@joshkopecek) <a href=\"https:\/\/twitter.com\/joshkopecek\/status\/552001401778229248\">January 5, 2015<\/a><\/p><\/blockquote>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<blockquote class=\"twitter-tweet\" lang=\"en\"><p>Not even 4 months since last <a href=\"https:\/\/twitter.com\/hashtag\/Internet?src=hash\">#Internet<\/a> issue in <a href=\"https:\/\/twitter.com\/hashtag\/Vietnam?src=hash\">#Vietnam<\/a> &#8211; <a href=\"https:\/\/twitter.com\/hashtag\/Slowing?src=hash\">#Slowing<\/a> down again&#8230; <a href=\"http:\/\/t.co\/ad91ZyGZrn\">http:\/\/t.co\/ad91ZyGZrn<\/a><\/p>\n<p>&mdash; Tanguy Sutunam (@tanguysutunam) <a href=\"https:\/\/twitter.com\/tanguysutunam\/status\/552009177015406593\">January 5, 2015<\/a><\/p><\/blockquote>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<blockquote class=\"twitter-tweet\" lang=\"en\"><p>This is the worst I&#39;ve ever known internet in Hanoi.<\/p>\n<p>&mdash; Steve Jackson (@ourman) <a href=\"https:\/\/twitter.com\/ourman\/status\/552003838433980417\">January 5, 2015<\/a><\/p><\/blockquote>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<blockquote class=\"twitter-tweet\" lang=\"en\"><p>&quot;RT <a href=\"https:\/\/twitter.com\/ngahpham\">@ngahpham<\/a>: All  Vietnamese soldiers should know how to swim by 2016, orders Defence minister&quot; so they can mend the internet cable?<\/p>\n<p>&mdash; Steve Jackson (@ourman) <a href=\"https:\/\/twitter.com\/ourman\/status\/552023609200214016\">January 5, 2015<\/a><\/p><\/blockquote>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><br \/>\n<\/center><\/p>\n<p><center><strong>See: <a href=\"https:\/\/www.techinasia.com\/10-trends-shape-southeast-asian-ecommerce-2015\/\">10 trends that will shape Southeast Asian ecommerce in 2015<\/a><\/strong><\/center><\/p>\n<p>(Source: <a href=\"http:\/\/www.thanhniennews.com\/tech\/transpacific-cable-snaps-slowing-down-internet-in-vietnam-37184.html\">Thanhniennews<\/a>)<\/p>\n<p>This post <a href=\"https:\/\/www.techinasia.com\/broken-undersea-cable-vietnam-internet-again-and-again-and-again\/\" title=\"Here we go again: broken undersea cable brings internet misery to Vietnam\">Here we go again: broken undersea cable brings internet misery to Vietnam<\/a> appeared first on Tech in Asia.<\/p>"
    }
}{
    "item": {
        "coverImg": "<div class=\\\"coverImg\\\" style=\\\"background:url('https:\/\/www-techinasia.netdna-ssl.com\/wp-content\/uploads\/2014\/09\/movietheater-720x431.jpg') no-repeat\\\"><\/div>",
        "title": "Baidu and Tencent\u2019s joint venture with Wanda nabs US$161 million",
        "desc": "Wanda Group's upcoming online-to-offline play is looking increasingly vague, and increasingly well-funded.",
        "link": "https:\/\/www.techinasia.com\/baidu-tencents-joint-venture-wanda-nabs-us161-million\/",
        "content": "<p><img class=\" size-large wp-image-192747 aligncenter\" src=\"https:\/\/www-techinasia.netdna-ssl.com\/wp-content\/uploads\/2014\/09\/movietheater-720x431.jpg\" alt=\"movietheater\" width=\"720\" height=\"431\" \/><\/p>\n<p>Today Wanda Group, the Chinese commercial real estate firm best known for its movie theater chains, announced that its joint ecommerce venture with Tencent and Baidu closed a RMB 1 billion (US$161 million)\u00a0funding round. The two investment funds, Xude Rendao and Centec Networks, will take\u00a02 percent and 3 percent equity in the company, respectively. Wanda Group\u00a0states that Tencent and Baidu now each hold a 15 percent stake in the venture, which is\u00a0now valued at RMB 20 billion (about US$3.2 billion).<\/p>\n<p>The Wanda-Tencent-Baidu ecommerce venture (which Wanda refers to as Wand E-commerce in its release) was publicly announced\u00a0<a href=\"https:\/\/www.techinasia.com\/tencents-baidus-814-joint-venture-wanda-signals-arrival-instore-wechat-payments\/\">back in August.<\/a>\u00a0Wanda maintained a 70 percent stake in the US$814 million venture, with Tencent and Baidu splitting the remainder evenly. While\u00a0the companies gave few specific details regarding the venture&#8217;s execution, their public statements repeatedly mentioned the importance of online-to-offline commerce. When one factor&#8217;s in Baidus commitment to\u00a0maps and beacon technology, WeChat&#8217;s commitment to payments and social, and Wanda&#8217;s entertainment properties, it&#8217;s easy to speculate &#8211; but hard to know specifically &#8211; how the venture might play out.<\/p>\n<p>Following the news of the venture&#8217;s formation, Wanda has kept quiet regarding its development. But\u00a0in December it <a href=\"https:\/\/www.techinasia.com\/tencent-baidubacked-wanda-buys-stake-alipay-competitor\/\">purchased a stake in 99Bill<\/a>, an Alipay competitor. That would appear to confirm Wanda&#8217;s interest in integrating online payments into its offline services, and\u00a0<a href=\"https:\/\/www.techinasia.com\/tencents-baidus-814-joint-venture-wanda-signals-arrival-instore-wechat-payments\/\">challenge previous assumptions<\/a> that Wanda would be hopping aboard the WeChat Payments train.<\/p>\n<p>Despite how little is known about the joint venture, Wanda looks set to\u00a0own a bevy of virtual real estate in addition to its physical real estate. &#8220;O2O places a greater importance on the link between people and services than traditional commercial methods, with the size of the market considerably greater than just physical transactions,&#8221; its statement reads. &#8220;Wanda possesses the world&#8217;s largest offline consumer network, with 1.5 million customers in 2014 and an estimated 6 billion customers by 2020. If Wanda E-commerce can combine its offline and online resources effectively, it has the potential to become a new Internet giant with incredible potential.&#8221; In other words, Wanda is thinking big.<\/p>\n<p>Wanda\u00a0adds that it will announce an additional funding round in late 2015.<\/p>\n<p style=\"text-align: center;\"><strong>See: <a href=\"https:\/\/www.techinasia.com\/tencent-baidubacked-wanda-buys-stake-alipay-competitor\/\">Wanda buys stake in Alipay competitor<\/a><\/strong><\/p>\n<p><em>Editing by Steven Millward, top image from <a href=\"https:\/\/www.flickr.com\/photos\/jessflickr\/127769116\/in\/photolist-chRiN-8FvMaK-6…JhB-6h1ktw-eJvmod-eJvseG-2QKFaC-2PF3gR-5bNmLN-9nFHuB-acLPgW-8Dqsco-9nFCHF\">jessflickr<\/a><\/em><\/p>\n<p>This post <a href=\"https:\/\/www.techinasia.com\/baidu-tencents-joint-venture-wanda-nabs-us161-million\/\" title=\"Baidu and Tencent&#8217;s joint venture with Wanda nabs US$161 million\">Baidu and Tencent&#8217;s joint venture with Wanda nabs US$161 million<\/a> appeared first on Tech in Asia.<\/p>"
    }
}

错误在 http://jsonlint.com/ 中说明

Parse error on line 9:
... Asia.<\/p>"    }}{    "item": {    
---------------------^
Expecting 'EOF', '}', ',', ']'

最佳答案

我猜你正在循环并回显每个数组..这并不能使它成为一个有效的 json 整体,因为 json 需要用 , 分隔数组中的每个项目有效。

我不知道您的 PHP 代码整体看起来如何,但也许您可以做这样的事情?

$output = array();
foreach($myArrays as $myArray)
{
    $content_arr = array();
    $myArray["item"]["content"] = $item_content;
    $output = array_merge($output, $myArray);
}
echo json_encode($output);

关于javascript - Ajax 回调返回了无效的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27777111/

相关文章:

php - 在数据库中保存javascript代码

PHP 显示每个项目的 foreach 输出到屏幕

javascript - 根据特定值拆分字符串,但保留分隔符,但这样做时不要将它们附加到任何东西上

javascript 在运行时删除函数以避免 "multicall"

javascript - 一些关于导航的 Jquery/CSS 帮助

php - 如何在PHP中随机组合两个数组

php - 没有选项,无法选择。如何在 Javascript 中调用数据库中的数据?

javascript - 从 firebase firestore 引用获取异步值

javascript - AJAX 结果发送到 app> 而不是 &lt;input&gt;

jquery - jQuery 中获取 Json 数据(对象中的对象)