0

I have following JSON string returned to a page.

{"firstName":"Bob","lastName":"Gates","department":"Tech"}

I would like to display properties values in a page but I'm having problem.

Here is my jquery ajax

function BuildTable(msg) {
           var Person = msg[0];
            var table = '<table><tr><td>' + person.firstName + '<td><tr></table>'
            $("#temp").html(table);
        };

  $.ajax({
                type: "POST",
                url: "../test/json.aspx/testjson",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    BuildTable(msg.d);
                }
            });

This returns "undefined" although I see JSON string in Firebug. I can return JSON string by using "msg" instead of "Person.firstName"

How do I display property values in JSON string?

Thank you

Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
shinya
  • 403
  • 2
  • 9
  • 23
  • This answer should help you out: http://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object – Darren Lewis Oct 11 '11 at 21:57

2 Answers2

0

Your JSON doesn't represent a javascript array, so you shouldn't be trying to get elements at a given index but directly use the properties of the object:

function BuildTable(msg) {
    var table = '<table><tr><td>' + msg.firstName + '<td><tr></table>'
    $("#temp").html(table);
}

Also your JSON object doesn't seem to have a .d property, so you could directly pass the object to the BuildTable function:

BuildTable(msg);

An array of persons would have looked like this:

[ {"firstName":"Bob","lastName":"Gates","department":"Tech"},
  {"firstName":"John","lastName":"Gates","department":"HR"} ]

Now you can access msg[0].firstName, ...

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

A couple things here that seem wrong

  1. you are doing msg.d, however the returned json object doesn't have a property named 'd'.

  2. once you pass in msg.d into your BuildTable function you try to use it as an array.

  3. you declare a Person object and then try to reference "person" with lowercase this is also incorrect since js is case sensitive.

UPDATE:

function BuildTable(msg) {
        var person = jQuery.parseJSON( msg );
        var table = '<table><tr><td>' + person.firstName + '<td><tr></table>'
        $("#temp").html(table);
    };
Keith.Abramo
  • 6,952
  • 2
  • 32
  • 46
  • I think I have "d" key. here is response section in firebug. {"d":"{\"firstName\":\"Bill\",\"lastName\":\"Gates\",\"department\":\"Tech\"}"} – shinya Oct 11 '11 at 22:22
  • Ah ok I see the response you put in your question was the one you were sending to your BuildTable function. In that case you don't need to index your msg and you need to fix your casing with the Person variable still. – Keith.Abramo Oct 11 '11 at 22:47
  • so, remove var Person and change it to msg.firstName should work, right? but I'm still getting undefined... – shinya Oct 11 '11 at 22:59
  • You still need to parse your string into a valid javascript object. I have updated my answer to reflect this. – Keith.Abramo Oct 11 '11 at 23:54