I am a javascript noob. I have a JSON string created by google gson API after creating the json string which I am passing it to my javascript function. So in a javascript variable I have a string as follows
'{"validationCode":"V10291","caseNumber":"2010CF101011","documentSource":"EFILE","countyDocumentID":"CD102","documentTitle":"D Title","signedDate":"01/01/2012","signedBy":"CPSJC","comments":"YES Comments"}'
How to iterate over this or get a value of the key something like I have to find validationCode or caseNumber, but this is String? Any suggestions are welcome
You can get it into a native JavaScript object with JSON.parse:
var obj = JSON.parse(yourJSONString);
Then you can iterate the keys with a standard for in loop
for(var k in obj)
if ({}.hasOwnProperty.call(obj, k))
console.log(k, " = ", obj[k]);
Or access particular keys like validationCode or caseNumber directly:
var caseNum = obj.caseNumber;
var validationCode = obj.validationCode;
Note that really old browsers don't support JSON.parse, so if you want to support them, you can either use Papa Crockford's json2, or jQuery, which has a parseJSON utility method.
You could do a for ... in to loop through the object properties:
var person={fname:"John",lname:"Doe",age:25};
var x;
for (x in person)
{
document.write(person[x] + " ");
}
http://www.w3schools.com/js/js_loop_for_in.asp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With