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

JavaScript & jQuery parse JSON in loop - Stack Overflow

programmeradmin4浏览0评论

Sorry about asking this, I can't get it to work even with looking at other questions...

I have a JSON output in "json.php", for example:

    [
       {"serverid":"1","servername":"Server One"},
       {"serverid":"2","servername":"Server Two"}
    ]

I have a script, to grab the data & parse it into a variable

var servers;
jQuery.get('json.php', function(data) {
     servers =    JSON.parse(data);
     jQuery('#servers').servers.servername
});

I have a div to output the results to:

<div id="servers"></div>

Whatever I try, I always get some kind of

"Uncaught TypeError: Cannot read property 'servername' of undefined" error.

I'd also like to look around the results, however I can't even get it to print atm.

Again sorry for another question like this

Sorry about asking this, I can't get it to work even with looking at other questions...

I have a JSON output in "json.php", for example:

    [
       {"serverid":"1","servername":"Server One"},
       {"serverid":"2","servername":"Server Two"}
    ]

I have a script, to grab the data & parse it into a variable

var servers;
jQuery.get('json.php', function(data) {
     servers =    JSON.parse(data);
     jQuery('#servers').servers.servername
});

I have a div to output the results to:

<div id="servers"></div>

Whatever I try, I always get some kind of

"Uncaught TypeError: Cannot read property 'servername' of undefined" error.

I'd also like to look around the results, however I can't even get it to print atm.

Again sorry for another question like this

Share Improve this question edited Mar 13, 2013 at 14:20 Mathew Thompson 56.4k15 gold badges129 silver badges150 bronze badges asked Mar 13, 2013 at 14:09 AliasAlias 3,0818 gold badges41 silver badges62 bronze badges 5
  • What is jQuery('#servers').servers.servername supposed to do??? Obviously, there is no servers property on jQuery objects. – Bergi Commented Mar 13, 2013 at 14:10
  • Well I think that's where the problem is, I want to be saying get the servername from the parsed data... no idea how to though. – Alias Commented Mar 13, 2013 at 14:12
  • did u check if data received from server is json array object? In that case you need to iterate though servers – smitrp Commented Mar 13, 2013 at 14:12
  • @Alias: There are two servernames in the response data. Which one do you want? – Bergi Commented Mar 13, 2013 at 14:13
  • @Bergi well I'd like both of them to be displayed one after the other, however at the moment I can't get anything to display. – Alias Commented Mar 13, 2013 at 14:15
Add a comment  | 

2 Answers 2

Reset to default 15

Don't you mean something like this? the jQuery object (which is a reference to your div) knows nothing about servername. Also, you'll need to iterate through the array of items in order to get them all:

servers = $.parseJSON(data);

$.each(servers, function(index, value) {
    $("#servers").text($("#servers").text() + " " + value.servername);
});

Fiddle: http://jsfiddle.net/H2RC2/1/

The error message is all you need. The jQuery('#servers') wrap the #servers div in the jQuery object. And this object has got no property such as servers.

Rather you could use:

    var servers = JSON.parse(data);
    var res = '';
    for(var i = 0; i<servers.length; i++){
        res = res + '<p>' + servers[i].servername +'</p>';
    }

    $('#servers').append(res);
发布评论

评论列表(0)

  1. 暂无评论