简体   繁体   中英

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.

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

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM