Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
161
rated 0 times [  167] [ 6]  / answers: 1 / hits: 105769  / 14 Years ago, fri, february 25, 2011, 12:00:00

Here's my json:



{d:{key1:value1,
key2:value2}}


Is there any way of accessing the keys and values (in javascript) in this array without knowing what the keys are?



The reason my json is structured like this is that the webmethod that I'm calling via jquery is returning a dictionary. If it's impossible to work with the above, what do I need to change about the way I'm returning the data?



Here's an outline of my webmethod:



<WebMethod()> _
Public Function Foo(ByVal Input As String) As Dictionary(Of String, String)
Dim Results As New Dictionary(Of String, String)

'code that does stuff

Results.Add(key,value)
Return Results
End Function

More From » asp.net

 Answers
44

You can use the for..in construct to iterate through arbitrary properties of your object:



for (var key in obj.d) {
console.log(Key: + key);
console.log(Value: + obj.d[key]);
}

[#93588] Wednesday, February 23, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kennedysaraiw

Total Points: 552
Total Questions: 99
Total Answers: 109

Location: South Sudan
Member since Sun, Jul 11, 2021
3 Years ago
;