json web service Get value from dynamic parameter's name

please i have this output from my json web service:

      {"format":"json",
       "success":true,
       "errors":[],
       "result":
       {"**206**":
         {"player_name":"stagiaire",
           "M":{
                "6480":{"score":0,"answer":"cdgvbdbgd","category_name":"cdgvbdbgd"},
                "6481":{"score":0,"answer":"cdgvbdbgd","category_name":"cdgvbdbgd"},
                "6482":{"score":0,"answer":"cdgvbdbgd","category_name":"cdgvbdbgd"},
                "6483":{"score":0,"answer":"cdgvbdbgd","category_name":"cdgvbdbgd"},
               },

          "O":{
                 ..... },
      }
     }
    } 

What i want is to extract the value (which is now 206 but it can be another number). I am asking for your help to achieve this goal. Any help will be appreciated.

Answers


In case the resulting object contains only a single property with this number as its name, i.e.

result = {
    206 : {
        ...
    }
    // no other properties here
}

then you can use something like:

var num = Object.keys(result).shift();  // "206"

Check the browser compatibility for Object.keys() method at MDN and use shim if needed.


If the object you look for always has a player name as value, try

for (var key in obj.result) if obj.result[key]["player_name") alert(key)

Or use jQuery

$.each(data.result,function(key,val) {
  alert(key);
});

Need Your Help

How to use iOS SDK 4.2 with Xcode 4.0.2

xcode4 ios-4.2

I installed Xcode4.0.2 yesterday and attempted to build and run a project developed by someone else, who was using an older version of Xcode (3.2.5) and iOS 4.2.

System::String^ cast to const char* does not store properly in an external variable

string forms c++-cli extern

I'm trying to obtain the name of a file in a Windows Forms object and then using that string at another c++ file. The problems comes out when sometimes it gets the correct name, and sometimes it do...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.