簡體   English   中英

如何在jQuery Mobile中解析嵌套的JSON

[英]how to parse nested json in jquery mobile

我在json //下方//除了星星在列表視圖中填充值//和星星在v單擊后顯示在下拉列表中,但無法解析該值//請幫助

"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循環或$。每次遍歷數組的值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');       
        }
    });    
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM