如何将json解码为数组

时间:2013-02-25 20:10:40

标签: php 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"
        } };

3 个答案:

答案 0 :(得分:2)

根据我的经验,在处理json_decode时,你永远不必大惊小怪引用引号。

我建议尝试json_decode( $b );

如果这不起作用,那么你的“json”实际上并不是有效的application/json数据,这就是解码失败的原因。

答案 1 :(得分: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));
?>

答案 2 :(得分:0)