将json转换为数组并获取数组值

时间:2014-07-05 04:53:46

标签: php arrays json

这里我尝试将json转换为数组,然后获取数组值。 我检查了SO线程并尝试了类似的方法,但仍然给出了错误:

Warning: Invalid argument supplied for foreach() in

<?php

$json = '[{"post_content": "nyone pls. guide me, for launching the website from where we can get the webspace and what should be the speed etc, the domain is booked through godaddy, Your technical guidance is appreciated. Thanks","name": "Uncategorized","taxonomy": "category"}, {"post_content": "Anyone know of a good Indian design firm for startups which also codes clean HTML and CSS (using technologies like sass, compass, grunt etc...)

Basically both great design + web development","name": "services","taxonomy": "discussion_type"}, {"post_content": "Anyone know of a good Indian design firm for startups which also codes clean HTML and CSS (using technologies like sass, compass, grunt etc...)

Basically both great design + web development","name": "Design","taxonomy": "discussion_tag"}, {"post_content": "Anyone know of a good Indian design firm for startups which also codes clean HTML and CSS (using technologies like sass, compass, grunt etc...)

Basically both great design + web development","name": "Front End","taxonomy": "discussion_tag"}, {"post_content": "Ajit Nazre (Harvard )Whole world is visiting what are you waiting for visit my website &amp; thanks for your visit !
get more updates on http://www.ajitnazre.com","name": "articles","taxonomy": "discussion_type"}, {"post_content": "Design Thinking for Startups.

Can a startup be designed specifically? Can you have a designer startup?
Maybe YES.

We are exploring the process of design thinking and how it applies to startups. Once complete, the process can be used to design a startup business, that will enable founders to take a correct roadmap, avoid traps and failures and post clear signs on strategy and direction.

Stay tuned.

#startupden","name": "Design Thinking","taxonomy": "discussion_tag"}, {"post_content": "Hello people! These are 5 questions, and will take less than a minute to fill up! This is really important for me. Thanks! 
https://docs.google.com/forms/d/1Axk7UZsZkiVXLroOk9Cc2wCgFZBTBWNOGgRU9nyYI1E/edit","name": "Survey","taxonomy": "discussion_tag"}, {"post_content": "Hello people! These are 5 questions, and will take less than a minute to fill up! This is really important for me. Thanks! 
https://docs.google.com/forms/d/1Axk7UZsZkiVXLroOk9Cc2wCgFZBTBWNOGgRU9nyYI1E/edit","name": "Food","taxonomy": "discussion_tag"}, {"post_content": "Please suggest best plagiarism tool other than smallseotool.com...","name": "Suggestion","taxonomy": "discussion_tag"}, {"post_content": "Please suggest best plagiarism tool other than smallseotool.com...","name": "Content","taxonomy": "discussion_tag"}, {"post_content": "Please suggest best plagiarism tool other than smallseotool.com...","name": "Tool","taxonomy": "discussion_tag"}, {"post_content": "Looking to buy short form video (30 seconds to 1 min ) across all genres . Should be original content only .","name": "buy-sell","taxonomy": "discussion_type"}, {"post_content": "Looking to buy short form video (30 seconds to 1 min ) across all genres . Should be original content only .","name": "Content","taxonomy": "discussion_tag"}]';

$arr = json_decode($json, TRUE);
foreach ($arr as $r)
    {
        echo $r['post_content'];
    }

?>

4 个答案:

答案 0 :(得分:1)

您的输入JSON无效,因此当您解码它时,您将无法获得数组NULL。 这是你的问题。 尝试var_dump($arr),您将获得NULL 要解决此问题,请避免更改行并将json放在一行中。

答案 1 :(得分:1)

你的json是不同的。它应该在一行中。见下文: -

<?php

    $json = '[{"post_content": "nyone pls. guide me, for launching the website from where we can get the webspace and what should be the speed etc, the domain is booked through godaddy, Your technical guidance is appreciated. Thanks","name": "Uncategorized","taxonomy": "category"}, {"post_content": "Anyone know of a good Indian design firm for startups which also codes clean HTML and CSS (using technologies like sass, compass, grunt etc...)Basically both great design + web development","name": "services","taxonomy": "discussion_type"}, {"post_content": "Anyone know of a good Indian design firm for startups which also codes clean HTML and CSS (using technologies like sass, compass, grunt etc...)Basically both great design + web development","name": "Design","taxonomy": "discussion_tag"}, {"post_content": "Anyone know of a good Indian design firm for startups which also codes clean HTML and CSS (using technologies like sass, compass, grunt etc...)Basically both great design + web development","name": "Front End","taxonomy": "discussion_tag"}, {"post_content": "Ajit Nazre (Harvard )Whole world is visiting what are you waiting for visit my website &amp; thanks for your visit !get more updates on http://www.ajitnazre.com","name": "articles","taxonomy": "discussion_type"}, {"post_content": "Design Thinking for Startups.Can a startup be designed specifically? Can you have a designer startup?Maybe YES.We are exploring the process of design thinking and how it applies to startups. Once complete, the process can be used to design a startup business, that will enable founders to take a correct roadmap, avoid traps and failures and post clear signs on strategy and direction.Stay tuned.#startupden","name": "Design Thinking","taxonomy": "discussion_tag"}, {"post_content": "Hello people! These are 5 questions, and will take less than a minute to fill up! This is really important for me. Thanks! https://docs.google.com/forms/d/1Axk7UZsZkiVXLroOk9Cc2wCgFZBTBWNOGgRU9nyYI1E/edit","name": "Survey","taxonomy": "discussion_tag"}, {"post_content": "Hello people! These are 5 questions, and will take less than a minute to fill up! This is really important for me. Thanks! https://docs.google.com/forms/d/1Axk7UZsZkiVXLroOk9Cc2wCgFZBTBWNOGgRU9nyYI1E/edit","name": "Food","taxonomy": "discussion_tag"}, {"post_content": "Please suggest best plagiarism tool other than smallseotool.com...","name": "Suggestion","taxonomy": "discussion_tag"}, {"post_content": "Please suggest best plagiarism tool other than smallseotool.com...","name": "Content","taxonomy": "discussion_tag"}, {"post_content": "Please suggest best plagiarism tool other than smallseotool.com...","name": "Tool","taxonomy": "discussion_tag"}, {"post_content": "Looking to buy short form video (30 seconds to 1 min ) across all genres . Should be original content only .","name": "buy-sell","taxonomy": "discussion_type"}, {"post_content": "Looking to buy short form video (30 seconds to 1 min ) across all genres . Should be original content only .","name": "Content","taxonomy": "discussion_tag"}]';

    $arr = json_decode($json, TRUE);

    foreach ($arr as $r)
        {
            echo $r['post_content'];
        }

    ?>

答案 2 :(得分:0)

这是有效的,试试这个

$aa= '[
    {
        "post_content": "nyone pls. guide me, for launching the website from where we can get the webspace and what should be the speed etc, the domain is booked through godaddy, Your technical guidance is appreciated. Thanks",
        "name": "Uncategorized",
        "taxonomy": "category"
    },
    {
        "post_content": "Anyone know of a good Indian design firm for startups which also codes clean HTML and CSS (using technologies like sass, compass, grunt etc...Basically both great design + web development",
        "name": "services",
        "taxonomy": "discussion_type"
    },
    {
        "post_content": "Anyone know of a good Indian design firm for startups which also codes clean HTML and CSS (using technologies like sass, compass, grunt etc...)Basically both great design + web development",
        "name": "Design",
        "taxonomy": "discussion_tag"
    },
    {
        "post_content": "Anyone know of a good Indian design firm for startups which also codes clean HTML and CSS (using technologies like sass, compass, grunt etc...)Basically both great design + web development",
        "name": "Front End",
        "taxonomy": "discussion_tag"
    },
    {
        "post_content": "Ajit Nazre (Harvard )Whole world is visiting what are you waiting for visit my website &amp; thanks for your visit !get more updates on http://www.ajitnazre.com",
        "name": "articles",
        "taxonomy": "discussion_type"
    },
    {
        "post_content": "Design Thinking for Startups.Can a startup be designed specifically? Can you have a designer startup?Maybe YES.We are exploring the process of design thinking and how it applies to startups. Once complete, the process can be used to design a startup business, that will enable founders to take a correct roadmap, avoid traps and failures and post clear signs on strategy and direction.Stay tuned.#startupden",
        "name": "Design Thinking",
        "taxonomy": "discussion_tag"
    },
    {
        "post_content": "Hello people! These are 5 questions, and will take less than a minute to fill up! This is really important for me. Thanks! https://docs.google.com/forms/d/1Axk7UZsZkiVXLroOk9Cc2wCgFZBTBWNOGgRU9nyYI1E/edit",
        "name": "Survey",
        "taxonomy": "discussion_tag"
    },
    {
        "post_content": "Hello people! These are 5 questions, and will take less than a minute to fill up! This is really important for me. Thanks! https://docs.google.com/forms/d/1Axk7UZsZkiVXLroOk9Cc2wCgFZBTBWNOGgRU9nyYI1E/edit",
        "name": "Food",
        "taxonomy": "discussion_tag"
    },
    {
        "post_content": "Please suggest best plagiarism tool other than smallseotool.com...",
        "name": "Suggestion",
        "taxonomy": "discussion_tag"
    },
    {
        "post_content": "Please suggest best plagiarism tool other than smallseotool.com...",
        "name": "Content",
        "taxonomy": "discussion_tag"
    },
    {
        "post_content": "Please suggest best plagiarism tool other than smallseotool.com...",
        "name": "Tool",
        "taxonomy": "discussion_tag"
    },
    {
        "post_content": "Looking to buy short form video (30 seconds to 1 min ) across all genres . Should be original content only .",
        "name": "buy-sell",
        "taxonomy": "discussion_type"
    },
    {
        "post_content": "Looking to buy short form video (30 seconds to 1 min ) across all genres . Should be original content only .",
        "name": "Content",
        "taxonomy": "discussion_tag"
    }
]';
$arr = json_decode($aa, TRUE);
foreach ($arr as $r)
    {
        echo $r['post_content'];
    }

答案 3 :(得分:0)

新行在json数据中无效。你应该将它们转换为有效的json。

试试这个

// convert new lines to corresponding json
$json = str_replace(array("\r", "\n"), array('\r','\n'), $json);

$arr = json_decode($json, TRUE);
foreach ($arr as $r)
    {
        echo $r['post_content'];
    }