2013-02-25 42 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