jquery - Get JSON and save as Variabel depending on ID -
i got following json example. if enter url: http://url.de#1234, content of kunde -> 1234 written variables. how can done?
json:
{ "kunde": { "1234": [{ "gender": "male", "firstname": "karl-heinz", }], "3214": [{ "gender": "female", "firstname": "annette-heinz", }], "5845": [{ "gender": "male", "firstname": "anton-heinz", }] } }
jsfiddle js , html: http://jsfiddle.net/7atle66g/
update
this work, not hash grabber , inserting json function. why not??
var url = window.location.href, idx = url.indexof("#"); var hash = idx != -1 ? url.substring(idx+1) : ""; var json_url = "http://localhost/test/beispiel.json"; $.getjson(json_url, function (data) { var t = data.kunde[hash][0]; alert(t.gender + " " + t.firstname); });
if you're traversing through child objects , unsure if exist can use hasownproperty method. not error. , error handling nice too. in ajax request can specify error function , catch unwanted side effects or maybe else wrap in try-catch block. when application ready should error free.
e. g. if(data.kunde.hasownproperty("1234")) ..
<script> var data = { "kunde": { "1234": [{ "gender": "male", "firstname": "karl-heinz", }], "3214": [{ "gender": "female", "firstname": "annette-heinz", }], "5845": [{ "gender": "male", "firstname": "anton-heinz", }] } }; window.onload = function() { var t = data.kunde["1234"][0]; alert(t.gender + " " + t.firstname); }; </script>
Comments
Post a Comment