2013-02-25 41 views
0

我有一个包含大量信息的Json数组。 JSON文本里面有单引号和双引号。 我试图将这个JSON解码为一个数组,因为json中的单引号和双引号。如何将json解码为数组

这意味着我需要放置在我的JSON之外,因此它可以编码,但我无法做到或者也许我的逻辑错误。任何人都可以帮助我尝试解码类似于这一个JSON的长列表吗?

我知道我必须使用json_decode('{my json info}');但我有把这些报价

$b= {"articles": [ 
     { 
      "content": "THE Queen last night toasted the UK's \"special relationship\" with the US \u2013 and told Barack Obama how their two nations are keeping the world safe.\\n\\nAt a state banquet held for the US President at Buckingham Palace last night, she thanked America for twice coming to the rescue of the \"free world\" in two world wars.\\n\\nShe said: \"Your visit to this country inevitably reminds us of our shared history, our common language, and our strong intellectual and cultural links.\\n\\n\"It also reminds us that your country twice came to the rescue of the free and democratic world when it was facing military disaster.\\n\\n\"On each occasion, after the end of those destructive wars, the generosity of the United States made a massive contribution to our economic recovery. Today the United States remains our most important ally and our two nations contribute to the security and prosperity of our peoples, and of the world, through shared national interests.\"\\n\\nShe told the assembled VIP guests that when the two countries stand together \"our people and other people of goodwill around the world will be more secure and can become more prosperous\".\\n\\nThe Queen finished with a toast, saying: \"Ladies and gentlemen, we are here to celebrate the tried, tested and \u2013 yes \u2013 special relationship between our two countries.\"\\n\\nIn response, Mr Obama praised the nations' bond \u2013 and said it had never been stronger in the post-9/11 world.\\n\\nHe said: \"Our relationship never rested as we approach the 10th anniversary of 9/11. I am particularly grateful for the solidarity that the UK has shown in the last decade. From that day to this you have been our closest partner.\"\\n\\nMr Obama paid particular tribute to the UK's military help in Iraq and Afghanistan.\\n\\nHe said: \"You have been our closest partner in the struggle to protect our people from terror attacks and violent extremists around the world, despite heavy sacrifices. Allow me to pay tribute to the considerable sacrifices of your military forces who have stood shoulder to shoulder with us for decades as we together faced the challenges of the 21st century.\"\\n\\nHe finished with a toast, saying: \"Ladies and gentlemen stand with me and raise your glasses as I propose a toast to Her Majesty the Queen. To the vitality of the special relationship between our peoples and in the words of Shakespeare, 'To this blessed plot, this earth, this realm, this England'.\"\\n\\nThe 170 VIP guests included former prime ministers and diplomats as well as Hollywood royalty including Tom Hanks and Kevin Spacey.\\n\\nEarlier in the day, the first of their three-day visit to the UK, Mr Obama and First Lady Michelle were given a 41-gun salute by the Royal Artillery as they arrived at the Palace.\\n\\nAs gusts of wind circled all around, they were greeted by the Queen and Prince Philip. Even though the salute was half a mile away in Green Park, the Queen joked about the noise, shaking her head to clear her ears.\\n\\nThe Obamas also met the Duke and Duchess of Cambridge before being taken on a tour of the Buckingham Palace picture gallery, where Mr Obama was shown letters and artefacts charting Britain's loss of the American colonies.\\n\\nHe peered over to look at a handwritten manuscript by George III from 1783 in which the King proclaimed: \"America is lost.\"\\n\\nMr Obama joked: \"That was only a temporary blip in the relationship.\"\\n\\nHe was then shown a photo of HMS Resolute, timbers from which have been recycled for use in the White House. \"This is now my desk in the Oval Office,\" he said. \"I think we got a pretty good deal out of that.\"\\n\\nSecurity for the presidential visit is ultra-tight \u2013 and even while they're staying at Buckingham Palace the Obamas are taking no chances.\\n\\nThey have brought their own bomb- and bullet-proof windows \u2013 installed in the Belgian Suite at the Palace.\\n\\nIn a packed day, Mr Obama also met David Cameron at Downing Street before they visited a South London school.", 
      "title": "The Queen toasts Barack Obama and special relationship with the US", 
      "source_url": "http://www.mirror.co.uk/news/top-stories/2011/05/25/the-queen-toasts-barack-obama-s-special-relationship-115875-23154933/", 
      "author": "Don Mackay", 
      "published": "25/05/2011" 
     } }; 
+0

在这情况下JSON字符串从何而来?为什么你必须将它嵌入到代码中? – deceze 2013-02-25 20:13:37

+0

它是我的代码中的文字 – 2013-02-25 20:14:50

回答

2

你错过了最后]

<?php 
$json =<<<A 
{"articles": [ 
     { 
      "content": "THE Queen last night toasted the UK's \"special relationship\" with the US \u2013 and told Barack Obama how their two nations are keeping the world safe.\\n\\nAt a state banquet held for the US President at Buckingham Palace last night, she thanked America for twice coming to the rescue of the \"free world\" in two world wars.\\n\\nShe said: \"Your visit to this country inevitably reminds us of our shared history, our common language, and our strong intellectual and cultural links.\\n\\n\"It also reminds us that your country twice came to the rescue of the free and democratic world when it was facing military disaster.\\n\\n\"On each occasion, after the end of those destructive wars, the generosity of the United States made a massive contribution to our economic recovery. Today the United States remains our most important ally and our two nations contribute to the security and prosperity of our peoples, and of the world, through shared national interests.\"\\n\\nShe told the assembled VIP guests that when the two countries stand together \"our people and other people of goodwill around the world will be more secure and can become more prosperous\".\\n\\nThe Queen finished with a toast, saying: \"Ladies and gentlemen, we are here to celebrate the tried, tested and \u2013 yes \u2013 special relationship between our two countries.\"\\n\\nIn response, Mr Obama praised the nations' bond \u2013 and said it had never been stronger in the post-9/11 world.\\n\\nHe said: \"Our relationship never rested as we approach the 10th anniversary of 9/11. I am particularly grateful for the solidarity that the UK has shown in the last decade. From that day to this you have been our closest partner.\"\\n\\nMr Obama paid particular tribute to the UK's military help in Iraq and Afghanistan.\\n\\nHe said: \"You have been our closest partner in the struggle to protect our people from terror attacks and violent extremists around the world, despite heavy sacrifices. Allow me to pay tribute to the considerable sacrifices of your military forces who have stood shoulder to shoulder with us for decades as we together faced the challenges of the 21st century.\"\\n\\nHe finished with a toast, saying: \"Ladies and gentlemen stand with me and raise your glasses as I propose a toast to Her Majesty the Queen. To the vitality of the special relationship between our peoples and in the words of Shakespeare, 'To this blessed plot, this earth, this realm, this England'.\"\\n\\nThe 170 VIP guests included former prime ministers and diplomats as well as Hollywood royalty including Tom Hanks and Kevin Spacey.\\n\\nEarlier in the day, the first of their three-day visit to the UK, Mr Obama and First Lady Michelle were given a 41-gun salute by the Royal Artillery as they arrived at the Palace.\\n\\nAs gusts of wind circled all around, they were greeted by the Queen and Prince Philip. Even though the salute was half a mile away in Green Park, the Queen joked about the noise, shaking her head to clear her ears.\\n\\nThe Obamas also met the Duke and Duchess of Cambridge before being taken on a tour of the Buckingham Palace picture gallery, where Mr Obama was shown letters and artefacts charting Britain's loss of the American colonies.\\n\\nHe peered over to look at a handwritten manuscript by George III from 1783 in which the King proclaimed: \"America is lost.\"\\n\\nMr Obama joked: \"That was only a temporary blip in the relationship.\"\\n\\nHe was then shown a photo of HMS Resolute, timbers from which have been recycled for use in the White House. \"This is now my desk in the Oval Office,\" he said. \"I think we got a pretty good deal out of that.\"\\n\\nSecurity for the presidential visit is ultra-tight \u2013 and even while they're staying at Buckingham Palace the Obamas are taking no chances.\\n\\nThey have brought their own bomb- and bullet-proof windows \u2013 installed in the Belgian Suite at the Palace.\\n\\nIn a packed day, Mr Obama also met David Cameron at Downing Street before they visited a South London school.", 
      "title": "The Queen toasts Barack Obama and special relationship with the US", 
      "source_url": "http://www.mirror.co.uk/news/top-stories/2011/05/25/the-queen-toasts-barack-obama-s-special-relationship-115875-23154933/", 
      "author": "Don Mackay", 
      "published": "25/05/2011" 
     }] 
} 
A; 


echo '<pre>'; 
print_r(json_decode($json, true)); 
?> 
+0

现在它的作品谢谢你 – 2013-02-25 20:20:54

2

以我的经验问题,你不应该用json_decode打交道时包裹报价大惊小怪。

我建议是尝试json_decode($b);

如果不工作,那么你的“JSON”实际上不是有效application/json的数据,这就是为什么解码失败。

+0

你可以测试它吗?复制上面的例子并附上配额并看看? – 2013-02-25 20:18:12

+0

当我说'不是真正有效的应用程序/ json数据'我的意思是你没有正确编码它开始。在这里,所有其他的答案已经解决了你的问题。不需要测试。 – Kristian 2013-02-25 20:19:57