-2

How to read this json data in jquery?

{"Contact":{"address1":"t","address2":"t","city":"t","state":"t","zip":"t"},"Profile":{"firstName":"t","lastName":"t"}}

  • 3
    possible duplicate of [Parse JSON in JavaScript?](http://stackoverflow.com/questions/4935632/parse-json-in-javascript) – Vogel612 Dec 04 '14 at 14:38
  • This question is really simple, but people have to start somewhere, and I'd rather find a question like this answered (as that would be useful), rather than stomped on... StackOverflow can't become the go-to place for finding answers without answering really simple questions... – Henry Crutcher Dec 05 '14 at 13:50

2 Answers2

0
var data = {"Contact":{"address1":"t","address2":"t","city":"t","state":"t","zip":"t"},"Profile":{"firstName":"t","lastName":"t"}}

data['Contact'].address1 //t
gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • Take a moment to read through the [editing help](http://stackoverflow.com/editing-help) in the help center. Formatting on Stack Overflow is different than other sites. – gunr2171 Dec 04 '14 at 17:13
0

I presume you got this string from somewhere, otherwise this wound't be a very interesting question....

var stringIGotAsWebReply = '{"Contact": ' +           
         '{"address1":"t","address2":"t","city":"t","state":"t","zip":"t"}, ' +
         ' "Profile":{"firstName":"t","lastName":"t"}}'
var obj = jQuery.parseJSON( stringIGotAsWebReply );

This is safer than eval, which is what the first question amounts to if you relax the assumption that the string is part of the program text. If the example read

var stringIGotAsWebReply = '{"Contact": ' +           
         '{"address1":"t","address2":"t","city":"t","state":"t","zip":"t"}, ' +
         ' "Evil":document.write("Script injection sux!"),
         ' "Profile":{"firstName":"t","lastName":"t"}}'
var obj = jQuery.parseJSON( stringIGotAsWebReply );

You'll have peace of mind because jQuery.parseJSON will fail rather than eval that. JSON.parse also is an option in many browsers, but jQuery calls that if available, so you may as well use the jQuery one...

Henry Crutcher
  • 2,137
  • 20
  • 28