Search code examples
javascriptjsonajaxhybrid-mobile-app

how to parse nested json in jquery mobile


I have below json // here except stars am populating value on list view // and stars am displaying in drop down after v click , but am not able to parse that value // Please help

"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"
},

// below code am using to parse the json and displaying it on 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");;
    }
}

// Please ignore English mistakes as am learning . Thanks


Solution

  • The below code will not work

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

    Because row.stars is an array , so you need to use another for loop or $.each to loop through the values of array row.stars to append to dropdown

    $(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');       
            }
        });    
    });