How to get JSON Key and Value?

后端 未结 3 1991
日久生厌
日久生厌 2021-01-30 08:28

I have written following code to get JSON result from webservice.

function SaveUploadedDataInDB(fileName) {
            $.ajax({
                type: \"POST\",
         


        
相关标签:
3条回答
  • 2021-01-30 09:09

    It looks like you're getting back an array. If it's always going to consist of just one element, you could do this (yes, it's pretty much the same thing as Tomalak's answer):

    $.each(result[0], function(key, value){
        console.log(key, value);
    });
    

    If you might have more than one element and you'd like to iterate over them all, you could nest $.each():

    $.each(result, function(key, value){
        $.each(value, function(key, value){
            console.log(key, value);
        });
    });
    
    0 讨论(0)
  • 2021-01-30 09:13
    $.each(result, function(key, value) {
      console.log(key+ ':' + value);
    });
    
    0 讨论(0)
  • 2021-01-30 09:34

    First, I see you're using an explicit $.parseJSON(). If that's because you're manually serializing JSON on the server-side, don't. ASP.NET will automatically JSON-serialize your method's return value and jQuery will automatically deserialize it for you too.

    To iterate through the first item in the array you've got there, use code like this:

    var firstItem = response.d[0];
    
    for(key in firstItem) {
      console.log(key + ':' + firstItem[key]);
    }
    

    If there's more than one item (it's hard to tell from that screenshot), then you can loop over response.d and then use this code inside that outer loop.

    0 讨论(0)
提交回复
热议问题