Am creating a script using JQuery on an ASP.NET page. Have a JSON string that looks like this:
<input id="hfImages" type="hidden" value="[
{"ProductID": "000001",
"Small": "http://myimages.com/images/I/75.jpg",
"Medium": "http://myimages.com/images/I/160.jpg",
"Large": "http://myimages.com/images/I/320.jpg",
"Thumb": "http://myimages.com/images/I/30.jpg"},
{"ProductID": "000002",
"Small": "http://myimages.com/images/I/75ab.jpg",
"Medium": "http://myimages.com/images/I/160j.jpg",
"Large": "http://myimages.com/images/I/320y.jpg",
"Thumb": "http://myimages.com/images/I/30ii.jpg"},
{"ProductID": "000003",
"Small": "http://myimages.com/images/I/75.jpg",
"Medium": "http://myimages.com/images/I/160.jpg",
"Large": "http://myimages.com/images/I/320.jpg",
"Thumb": "http://myimages.com/images/I/30.jpg"}
]"/>
I create an object from this string:
var images = $.parseJSON($("#[id*='hfImages']").val());
Have a var called ProductID that contains the value of the "ProductID" I need to get from this JSON string, but I don't know how to do it. Eventually (once I've retrieved the right data from this string), I'd want to be able to do something like this:
$("#productImages img.small").src(image[0].Small)
$("#productImages img.medium").src(image[0].Medium)
$("#productImages img.large").src(image[0].Large)
$("#productImages img.thumb").src(image[0].Thumb)
I've seen examples that loop through the JSON object until it finds the desired 'key', but I'm thinking there is a simpler way of doing this with .filter or .map?
Am very new to Javascript/JQuery and JSON. Seems like I have a key called "ProductID", but everything I'm reading doesn't refer to a single key. As though, every Name in the Name/Value is considered a key.
My problem finding a good example of how to do this probably has to do with my not knowing the right terminologies for everything involved here.
Can someone show me an example in JQuery of how to get an object from the above JSON, given that the desired ProductID is stored as a string in var ProductId? (var ProductID = "000002";)
I would also appreciate any links to good references on JSON and JQuery. I've been to json.org and several others, but they're not very helpful at my level of knowledge. :S