如何在 jquery 移动设备中解析嵌套的 json

how to parse nested json in jquery mobile

我有以下json // 这里除了星星之外正在填充列表视图中的值 // 星星在 v click 后显示在下拉列表中,但我无法解析该值 // 请帮忙

"entries": [
  {
"Doctor_Criteria_2": "Physician",
"Status": "Completed",
"Comment": "Testing",
"Timeline_For_Reporting": "1 month",
"Doctor_Criteria_1": "10",
"Speciality": "1",
"Faculty_No": "2",
"stars": [
  "Sumit",
  "Kumar",
  "Saini"
],
"Event_Id": "1503209071",
"Speaker_No": "2",
"End_Date_Time": "2017-08-25T10:00",
"Budget_Allocation": "2017-08-26T10:00",
"No_Of_Doctors_Assign": "Barbiturates",
"Doctor_Assignment": "Physician",
"Actual_Budget": "15000",
"Start_Date_Time": "Enthuse",
"Event_Name": "Med_Vision",
"Product_List": "10000",
"Assign_Team_Name": "0",
"Modular_No": "3"
},

// 下面的代码用于解析 json 并将其显示在 listview

$(document).on('pageinit', '#home', function(){      
//    var url = 'http://api.themoviedb.org/3/',
//        mode = 'search/movie?query=',
//        movieName = '&query='+encodeURI('Batman'),        
//        key = '&api_key=5fbddf6b517048e25bc3ac1bbeafb919';    
    var url = 'https://usv.mybluemix.net/USV/Json.jsp';

    $.ajax({
//        url: url + mode + key + movieName ,
        url: url,
        dataType: "json",
        async: true,
        success: function (result) {
             $('#work-in-progress').fadeOut(0);
            ajax.parseJSON(result);


        },
        error: function (request,error) {
            alert('Network error has occurred please try again!');

            document.getElementById("internet_access").innerHTML="No internet access";
        }
    });         
});

$(document).on('pagebeforeshow', '#headline', function(){      
    $('#Doctor_Name').empty();
    $.each(movieInfo.result, function(i, row) {
        if(row.Event_Creation_Id == movieInfo.id) {
   // here i want to display stars value in drop down in Doctor_Name
            $('#Doctor_Name').append(' <option value="'+row.stars+'">'+row.stars+'</option>');
             document.getElementById("user").value =USER_NAME;

         // document.getElementById("Store_name").value = row.Event_Name;

          //  $("#Doctor_Name").select2("val", "");
            $('#Doctor_Name').selectmenu('refresh');       
        }
    });    
});

$(document).on('vclick', '#movie-list li a', function(){  
    movieInfo.id = $(this).attr('data-id');
    $.mobile.changePage( "#headline", { transition: "slide", changeHash: false });
});

var movieInfo = {
    id : null,
    result : null
}

var ajax = {  
    parseJSON:function(result){  
        movieInfo.result = result.entries;
        $.each(result.entries, function(i, row) {

            if (row.Status != "Completed") {
            //console.log(JSON.stringify(row));
            $('#movie-list').append('<li><a href="" data-id="'  + row.Event_Creation_Id + '">' +'<h3>' + row.Event_Name + '</h3><p1>' + row.Event_Creation_Id + '</p1><br><p2>' + row.Start_Date_Time +'</p2><p2>'+row.End_Date_Time + '</p2><p>'+row.Status +'</p></a></li>');
        }});
        $('#movie-list').listview('refresh').trigger("create");;
    }
}

// 学习中请忽略英文错误。 谢谢

像这样修改 parseJSON 以查看 stars

parseJSON: function(result) {
  movieInfo.result = result.entries;
  $.each(result.entries, function(i, row) {
    if (row.stars && row.stars.length) {
      row.stars.forEach(function(s) {
         $('#Doctor_Name').append($('<option value="'+s+'">'+s+'</option>'));
      });
    }
    if (row.Status != "Completed") {
      $('#movie-list').append('<li><a href="" data-id="' + row.Event_Creation_Id + '">' + '<h3>' + row.Event_Name + '</h3><p1>' + row.Event_Creation_Id + '</p1><br><p2>' + row.Start_Date_Time + '</p2><p2>' + row.End_Date_Time + '</p2><p>' + row.Status + '</p></a></li>');
    }
  });
  $('#movie-list').listview('refresh').trigger("create");;
}
}

下面的代码将不起作用

 $('#Doctor_Name').append(' <option value="'+row.stars+'">'+row.stars+'</option>'); 

因为row.stars是一个数组,所以你需要使用另一个for循环或$.each循环遍历数组row.stars的值以附加到下拉列表

$(document).on('pagebeforeshow', '#headline', function(){      
    $('#Doctor_Name').empty();

    $.each(movieInfo.result, function(i, row) {
        if(row.Event_Creation_Id == movieInfo.id) {
   // here i want to display stars value in drop down in Doctor_Name
          if(row.stars && row.stars.length > 0)
          {

            for(var i =0;i<row.stars.length;i++)
              {
                 $('#Doctor_Name').append(' <option value="'+row.stars[i]+'">'+row.stars[i]+'</option>');
              }

        }
        document.getElementById("user").value =USER_NAME;

         // document.getElementById("Store_name").value = row.Event_Name;

          //  $("#Doctor_Name").select2("val", "");
            $('#Doctor_Name').selectmenu('refresh');       
        }
    });    
});