How to read this json data in jquery?
{"Contact":{"address1":"t","address2":"t","city":"t","state":"t","zip":"t"},"Profile":{"firstName":"t","lastName":"t"}}
How to read this json data in jquery?
{"Contact":{"address1":"t","address2":"t","city":"t","state":"t","zip":"t"},"Profile":{"firstName":"t","lastName":"t"}}
var data = {"Contact":{"address1":"t","address2":"t","city":"t","state":"t","zip":"t"},"Profile":{"firstName":"t","lastName":"t"}}
data['Contact'].address1 //t
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...