tags:

views:

21

answers:

1

Hi,

Very simple but didn't have enough time to do a quick search. Kindly help me with the following.

public class myKeyValue
{
    public int myKey { get; set; }
    public int myValue { get; set; }
}

IList<myKeyValue> is what gets javascript serialized.

Following is what browser gets in one of its in-memory variable.
[{"myKey":1,"myValue":7},
{"myKey":2,"myValue":286},
{"myKey":3,"myValue":200},
{"myKey":4,"myValue":176}] 

Now I simply want to access json value for the passed-in key. For example I pass 3 as a key and it returns me 200. I do not want to do it inside loop (that also will work, if there's no other way around). Kindly reply at the earliest.

I would really appreciate your help.

Thanks in advance.

A: 

in Javascript :

    yourJson = [{"myKey":1,"myValue":7},
                {"myKey":2,"myValue":286},
                {"myKey":3,"myValue":200},
                {"myKey":4,"myValue":176}]

   alert(yourJson[3].myValue); //will output 176
   alert(yourJson["3"].myValue);//will output 176
   alert(yourJson[3].myKey);//will output 4
   alert(yourJson["3"].myKey);//will output 4

And if you need hashmap in javascript look at previous answers. You best bet may be to loop through and check keyValue. Or if possible just change to zero based on yr original map.

NimChimpsky
It worked ! A big thanks !
Gurdeep
Hey, it didn't work :( Its is taking "index" as a key. But I want the the value based upon the actual key being passed. Any idea ?
Gurdeep
@Gurdeep see updated answer
NimChimpsky