1

I am generating JSON via an Ajax loop which I am successfully iterating and getting results. I need only the first index value of JSON which is name and I am doing this as in jQuery:

PHP

 $jsonRows[] = array(
            "name" => $result['name'],
            "datetime" => $result['datetime'],
            "place" => $result['place'],

        );
print_r(json_encode($jsonRows));

suppose the values are coming:

name: raj,
datetime: 2013-03-01 16:50:21,
place: India

name: jatin,
datetime: 2013-03-01 20:50:21,
place: US

name: raman,
datetime: 2013-03-03 01:50:21,
place: Japan

I need only name: raj but I am not getting this value:

JavaScript

$.each(response, function(i, item) {
       alert(item(0).name);
    });

error: object is not a function

3
  • 1
    why not response[0].name Commented Mar 4, 2013 at 13:58
  • is response an array? can you check the result for console.log(result) in the console. Add it before the $.each loop Commented Mar 4, 2013 at 14:01
  • sorry for wrong comment. i deleted my comment. Commented Mar 4, 2013 at 14:04

1 Answer 1

4

try this

$.each(response, function(i, item) {
   alert(item.name);
});

example fiddle here

updated

if you need just the first one only thn no need of loop..

alert(response[0].name);

updated fiddle

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

3 Comments

each() is loop in j query and it will return all the index which have name key. I need only first name:raj
ya your fiddle returning all the names.. but not only one.. which is raj
ok i got...try this..response[0].name.. if you need only one then no need of loop each..jsfiddle.net/bipen/L9Zfx/3

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.