1

I'm having this code in a php file

[
{
    "_id": "549f065925e1928098c74275",
    "index": 0,
    "guid": "c21b3720-430e-4be3-9309-e8afbabc0020",
    "email": "[email protected]",
    "phone": "+1 (804) 417-2615",
    "address": "657 Temple Court, Katonah, Mississippi, 7139",
    "about": "Ut laborum ut nostrud dolore ut aute irure aliquip duis. Amet proident fugiat cupidatat nulla ullamco adipisicing ea excepteur. Proident in ullamco aute reprehenderit ea. Consequat non cupidatat id sit nostrud non. Velit amet exercitation incididunt aliqua deserunt cupidatat et ex.\r\n",
    "registered": "2014-09-05T01:48:12 -03:00",
    "latitude": -44.882099,
    "longitude": 24.574332,
    "tags": [
        "officia",
        "consectetur",
        "incididunt",
        "eu",
        "magna",
        "esse",
        "elit"
    ],
    "friends": [
        {
            "id": 0,
            "name": "Billie Jarvis"
        },
        {
            "id": 1,
            "name": "Laurie Espinoza"
        },
        {
            "id": 2,
            "name": "Kate Stuart"
        }
    ],
    "greeting": "Hello, Browning Riley! You have 6 unread messages.",
    "favoriteFruit": "banana"
},
{
    "_id": "549f065925aa17df2fd6ebea",
    "index": 1,
    "guid": "9cf247e8-fe6b-4c42-a4a3-24ef7b907ad4",
    "email": "[email protected]",
    "phone": "+1 (946) 506-2141",
    "address": "414 Willoughby Avenue, Gila, California, 4696",
    "about": "Aliqua aute tempor veniam sit esse velit anim. Proident amet aliqua ad non labore eu voluptate labore in amet exercitation irure. Qui laborum ea aliqua consectetur minim aliqua amet minim laborum sint fugiat ullamco nulla elit.\r\n",
    "registered": "2014-02-11T20:29:39 -02:00",
    "latitude": -19.03677,
    "longitude": 138.137275,
    "tags": [
        "eu",
        "non",
        "et",
        "nostrud",
        "enim",
        "proident",
        "sint"
    ],
    "friends": [
        {
            "id": 0,
            "name": "Gamble Porter"
        },
        {
            "id": 1,
            "name": "Jami Bell"
        },
        {
            "id": 2,
            "name": "Mullen Alexander"
        }
    ],
    "greeting": "Hello, Bonita Sharp! You have 5 unread messages.",
    "favoriteFruit": "strawberry"
}
]

Now trying to extract data from this string as follows

 $("#get").on('click',function(){

            $.get('data.php',function(data){
                console.log(data);
                var json_array = data;
                var new_array = [];
                $.each(json_array,function(i,o) {
                 new_array.push(o._id);
                });

                console.log(new_array);

        });
    });

But this is the result "TypeError: invalid 'in' operand a" as you can see here in this online example. the php file is data.php and the code is in the script.js file

3
  • I sincerely hope that that information is not real data of actual people! Commented Dec 27, 2014 at 20:31
  • no it's from here json-generator.com just random not real json data Commented Dec 27, 2014 at 20:32
  • 2
    Use $.getJSON instead of $.get. Commented Dec 27, 2014 at 20:32

2 Answers 2

3

All you need to do is set the dataType as 'json' or use $.getJSON() which sets the dataType automatically.

This will tell $.ajax what to expect and to parse it accordingly

     $.get('data.php',function(data){

                var myarray = data;
                console.log(myarray);
                var new_array = [];
                $.each(myarray,function(i,o) {
                 new_array.push(o._id);
                });

                console.log(new_array);


            $("#result").text(new_array);
        },'json');/* last argument is "dataType" */

The content header for the file is not being sent as application/json

DEMO

Sign up to request clarification or add additional context in comments.

Comments

2

You need to parse your php response:
data = JSON.parse(data)
See this plunker (line 7 was updated)

The reason is that you are getting a string in the response, then you need to parse it to JSON, you do so by the above example.

3 Comments

the benefit of letting jQuery do the parsing internally is you don't need to create your own try/catch in case of invalid json string rather it will trigger an ajax error callback instead
@charlietfl, you are right but it is better to understand what is going on behind jquery (little of it)
but that's part of my point, using the internals to help catch errors without having to catch them yourself. Your solution doesn't cover the extra error handling that would be needed

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.