最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - json first index value get in jquery - Stack Overflow

programmeradmin0浏览0评论

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 ing:

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

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 ing:

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

Share Improve this question edited Mar 28, 2016 at 21:57 GʀᴜᴍᴘʏCᴀᴛ 9,04820 gold badges90 silver badges160 bronze badges asked Mar 4, 2013 at 13:52 NareshNaresh 2,78110 gold badges48 silver badges80 bronze badges 3
  • 1 why not response[0].name – Arun P Johny 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 – Arun P Johny Commented Mar 4, 2013 at 14:01
  • sorry for wrong ment. i deleted my ment. – Naresh Commented Mar 4, 2013 at 14:04
Add a ment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论