locked
iterate json array in jquery RRS feed

  • Question

  • User-1241097802 posted

    I have Json Format something like this. How i can perform For loop using this format.

    {"d":
    "{\"Table\":
    
    [{\"ImageName\":\"$parent in angularjs.png\",\"ImagePath\":\"~/Administration/imageHotspot/$parent in angularjs.png\",\"HotSpotID\":11,\"XCordinate\":\"1146\",\"YCordinate\":\"590\"},
    
    {\"ImageName\":\"$parent in angularjs.png\",\"ImagePath\":\"~/Administration/imageHotspot/$parent in angularjs.png\",\"HotSpotID\":11,\"XCordinate\":\"1216\",\"YCordinate\":\"588\"},
    
    {\"ImageName\":\"$parent in angularjs.png\",\"ImagePath\":\"~/Administration/imageHotspot/$parent in angularjs.png\",\"HotSpotID\":11,\"XCordinate\":\"1188\",\"YCordinate\":\"626\"},
    
    {\"ImageName\":\"$parent in angularjs.png\",\"ImagePath\":\"~/Administration/imageHotspot/$parent in angularjs.png\",\"HotSpotID\":11,\"XCordinate\":\"1162\",\"YCordinate\":\"582\"},
    
    {\"ImageName\":\"$parent in angularjs.png\",\"ImagePath\":\"~/Administration/imageHotspot/$parent in angularjs.png\",\"HotSpotID\":11,\"XCordinate\":\"1193\",\"YCordinate\":\"586\"}]}"}

    Here is the code which is send from back-end to jquery.

     [WebMethod()]
            public static string GetImageData(string dataSecName)
            //public static string GetRole()
            {
                clsSection objSectNew = new clsSection();
                DataSet DS = new DataSet();
                DS = objSectNew.GetImageDataByJQ(dataSecName);
                return JsonConvert.SerializeObject(DS);
       
    
            }

    I want to perform the Looping operation. can you please help me.

    Saturday, February 15, 2020 12:39 PM

Answers

  • User281315223 posted

    Generally, you don't want to handle explicitly serializing your entire dataset and sending that down (it's preferable to build a collection of objects and just return it, the built-in serializer will do the rest). 

    With that said, if you wanted to access the JSON content via jQuery and iterate through them, you'll need to access the 'd' object from the response and then explicitly parse it as JSON, which will allow you to iterate through it:

    // Read in your name you want to pass up
    var dataSecName = '...';
    // Make your request
    $.ajax({
        type: 'POST',
        url: '<%= ResolveUrl("~/YourPage.aspx/GetImageData") %>?dataSecName=' + dataSecName,
        contentType: 'application/json; charset=utf-8',
        success: function (response) {
            // 'd' will contain your message
            val json = JSON.parse(response.d);
    // Iterate through your table (e.g. your images $.each(json.Table, function(index, image) { // Do something here }); } });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 16, 2020 5:33 AM

All replies

  • User281315223 posted

    Generally, you don't want to handle explicitly serializing your entire dataset and sending that down (it's preferable to build a collection of objects and just return it, the built-in serializer will do the rest). 

    With that said, if you wanted to access the JSON content via jQuery and iterate through them, you'll need to access the 'd' object from the response and then explicitly parse it as JSON, which will allow you to iterate through it:

    // Read in your name you want to pass up
    var dataSecName = '...';
    // Make your request
    $.ajax({
        type: 'POST',
        url: '<%= ResolveUrl("~/YourPage.aspx/GetImageData") %>?dataSecName=' + dataSecName,
        contentType: 'application/json; charset=utf-8',
        success: function (response) {
            // 'd' will contain your message
            val json = JSON.parse(response.d);
    // Iterate through your table (e.g. your images $.each(json.Table, function(index, image) { // Do something here }); } });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 16, 2020 5:33 AM
  • User-1241097802 posted

    Thank you sir

    Sunday, February 16, 2020 1:24 PM