0

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 

4 Answers 4

2

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", } 
Sign up to request clarification or add additional context in comments.

1 Comment

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!
1

Your data is an array.

So you'd want

$("#output").append(data[0].rep1FullName); 

1 Comment

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!
1

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" } 

1 Comment

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!
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.