1

I have a JSON string like this:

{"{\"nodeName\":\"abc\"}":[{"url":"abc","status":true},{"url":"abc","status":true}],"      {\"nodeName\":\"pqr\"}":[{"url":"abc","status":true},{"url":"abc","status":true}]}

i am able to get the value(v) using below query code but not able to get key(k). On alert i am getting (undefined abc true). value of k is undefined.

 $.each(obj, function() {
      $.each(this, function(k, v) {
    alert(k.nodeName +" "+ v.url +" "+v.status);
     });
    });

I need value of (k) for further validation. I've validated the JSON string to make sure it was valid, so what am I missing here or is there any other way of doing this?? Please help...

Also, From server end i am passing json object like this:

json.put(js.toString(),jsarray)

where

json = json OBject,
js = json Object,
jsarray = json Array.
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Manan Kapoor
  • 675
  • 1
  • 11
  • 28
  • Keys are always string. In this case it looks like the key contains JSON itself. You have to parse the JSON into an object first. See http://stackoverflow.com/q/4935632/218196. But honestly, this is a very confusing data structure. It would be better to use the node name directly as key, i.e. your JSON would look like `{"abc": [...], "pgr": [...], ...}`. – Felix Kling Jun 18 '13 at 08:50
  • I have changed the json string as below: {"abc":[{"url":"asd","status":true},{"url":"as","status":true}],"xyz":[{"url":"as","status":false},{"url":"as","status":true}]} but i need the name of key as abc,xyz but while iterating it is giving as 0 ,1 – Manan Kapoor Jun 18 '13 at 09:00
  • Have a look at my answer. In the inner `$.each` you iterating over the value, i.e. the array. The key you want to have is from the outer `$.each` loop. – Felix Kling Jun 18 '13 at 09:05

1 Answers1

1

Keys are always string. In this case it looks like the key contains JSON itself. You have to parse the JSON into an object first. See Parse JSON in JavaScript?.

But honestly, this is a very confusing data structure. It would be better to use the node name directly as key, i.e. your JSON would look like {"abc": [...], "pgr": [...], ...}.

Then you would access the data like so:

$.each(obj, function(key, arr) {
    $.each(arr, function(_, v) {
        alert(key +" "+ v.url +" "+v.status);
    });
});
Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • thanks @Felix Kling.... it worked for me. i was looking for this solution since last 2 days. thanks a lot :) – Manan Kapoor Jun 18 '13 at 09:10
  • You're welcome! When you get an answer that solves your problem, please click on the checkmark next to it to "accept" it (you can always only accept one answer). This also signals others that this question is solved. – Felix Kling Jun 18 '13 at 09:11
  • this solution worked for me. i am able to get the value in alert but when i am trying to set the value in jstl:set, it is not working. but key is getting set in jstl:set – Manan Kapoor Jun 18 '13 at 11:44
  • I don't know what `jstl:set` is so I cannot really help you there. Sorry. – Felix Kling Jun 18 '13 at 11:45
  • np, but can u please tell me how to convert the values of json array(v.url and v.status) in to javascript string ??? – Manan Kapoor Jun 18 '13 at 12:19
  • First of all, `v` is not a "JSON array" it is a JavaScript object. At the moment you are accessing the data, you don't have JSON anymore. The JSON was already parsed into arrays and objects. `v.url` and `v.status` are already strings. – Felix Kling Jun 18 '13 at 12:24