Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tlug] [Javascript] Looping through JSON data should be simple, shouldn't it?



TLUG,

I've got a JSON object that I retrieve from a PHP script that has a list
of emails and usernames.

All I want to do is loop through that data to echo out to the browser
each set of user data, along with a little formatting.

The content of the JSON looks like this:
"requestList"{ "1"{"username" : "test1", "email" : "test@example.com"},
"2"{"username" : "test2", "email" : "test2@example.com;

So far as I can tell from other tests, it is properly formatted (in case
I made a copy/paste error above).

>From looking on the internet, I should be able to loop through the JSON
in Javascript using a for loop, and calling on the "length" property.
Like so:

$.post('jsonhandler.php', {JSON: JSON.stringify(Data)}, checkResult,
"json");
function checkResult(returnJSON)
{
    for (var i = 0 ; i < returnJSON.requestList.length ; i = i + 1)
    {
        document.write('Hello world!');
    }
}

However, I'm not getting my "Hello world!" response back.

If I do this:
console.log("the length is " + requestListResult.requestList.length);

... in my Firebug console, I get: "the length is undefined".

I started to think something was fundamentally wrong with my approach
when I was writing this message, because I realized that even if I did
successfully loop through the JSON data, I wouldn't know how to specify
the username and email in order to output them:

document.write( "Username is: " + // what would I put here?//)

Any advice on what I'm doing wrong here would be much appreciated.

-- 
Dave M G


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links