1

When I'm trying to decode the JSON string to the array, it decodes only one entry from the entire string.

    public function GetAllCommunities() {
        $json_url = "URL";
        $json_name = "NAME";

        $returnArray = null;

        $curl_init = curl_init();
        curl_setopt($curl_init, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl_init, CURLOPT_URL, $json_url);
        $result = curl_exec($curl_init);
        curl_close($curl_init);

        //print_r($result);

        $jsonArray2 = json_decode($result, true);

        print_r($jsonArray2);
    }

When I print_r the result, I get all the data, but when I do decode the json_decode($result, true);, I get only 1 entry from the entire feed.

Any idea why? Could this mean there's error in the feed?

I have nested JSONs there, and I'm checking their correctness by using this: http://json.parser.online.fr/

Thanks.

1
  • 3
    You can't have duplicate entries. In your JSON string, you have Community repeated several times. Check here: jsonlint.com Commented Sep 9, 2014 at 14:33

1 Answer 1

4

Your JSON is malformed. You have several objects on the same level with the same name (Comnunity).

Instead of

[
    {
        Comunity: ... ,
        Comunity: ... ,
        Comunity: ... ,
    }
]

Do:

[
    {
        Comunity: ... 
    },
    {
        Comunity: ...
    },
    {
        Comunity: ...
    }
]
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you much. I would never catch this one haha
There are several possible ways, but the one he tries is def not working :D
@scragar: I think he's getting the JSON structure from an external feed, so he probably wouldn't be able to edit the structure :p
I don't construct the feed, I'm just using it. thanks anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.