tags:

views:

42

answers:

4

Hello, I am trying to parse some very basic JSON, but I don't know where I'm going wrong when trying to display it to the screen.

Am I not GRABBING the data correctly, such as, "data.re1Code"? I hope someone can shed some light onto my basic question sorry.

JSON Data

[
    {
        "rep1FullName": "Justin Giesbrecht",
        "rep1Code": "dc",
    }
]

Javascript

$.ajax({
  type: "GET",
  url: "testJSONData.php",
  dataType: "json",
  success: function(data) {

  $("#output").append(data.rep1FullName);

},
  error: function () { alert("Error"); }
}); // End of generated json 
+1  A: 

Your data is an array.

So you'd want

$("#output").append(data[0].rep1FullName);
sje397
Well the reason i'm getting the data that way, is how PHP uses json_encode(). Never thought about that issue. Thank you that worked great!
Justin
+1  A: 

You are returning a jSon array so you would need to access it via data[0].rep1FullName or return the jSon as below and then use data.rep1FullName

{
    "rep1FullName":"Justin Giesbrecht",
    "rep1Code":"dc"
}
Nalum
Well the reason i'm getting the data that way, is how PHP uses json_encode(). Never thought about that issue. Thank you that worked great!
Justin
+1  A: 

The brackets [] make data a JSON array with your object as the 0th element so to get "Justin Giesbrecht" use the code: $("#output").append(data[0].rep1FullName); or remove the brackets and make the JSON:

{
        "rep1FullName": "Justin Giesbrecht",
        "rep1Code": "dc",
    }
Adam
Well the reason i'm getting the data that way, is how PHP uses json_encode(). Never thought about that issue. Thank you that worked great!
Justin
+1  A: 

Also, remove the last comma from the object notation.

[
    {
        "rep1FullName": "Justin Giesbrecht",
        "rep1Code": "dc" // <-- No comma, breaks in IE if you have a comma.
    }
]

Some of the other posters did this, but didn't mention it.

Sandro
Good eye captain!
Justin