locked
Loop through json result using Json.NET RRS feed

  • Question

  • User969992919 posted

    Hi,

    I am using below code to get list of halls using JSON.NET.

    I want to know how can I loop through the result to get hall_name?

    Here is my code followed by sample result.

    var client = new HttpClient();
    client.BaseAddress = new Uri("https://www.domain.com/ws/populate_halls_in_location.php");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage response = await client.GetAsync("https://www.domain.com/ws/populate_halls_in_location.php");
    
    if (response.IsSuccessStatusCode)
    {
        var data = await response.Content.ReadAsStringAsync();
    
        dynamic storydata = JsonConvert.DeserializeObject(data);
    
        *************************
        *************************
        *************************
    
        LOOP THROUGH THE RESULT FROM storedata to get hall_name
    
        *************************
        *************************
        *************************
    };
    

    here is ample output:

    [  
       {  
          "hall_id":1,
          "0":1,
          "site_id":7,
          "1":7,
          "hall_name":"Hall No. 1",
          "is_active":1,
          "3":1
       },
       {  
          "hall_id":2,
          "0":2,
          "site_id":7,
          "1":7,
          "hall_name":"Hall No. 2",
          "is_active":1,
          "3":1
       },
       {  
          "hall_id":3,
          "0":3,
          "site_id":7,
          "1":7,
          "hall_name":"Hall No. 3",
          "is_active":1,
          "3":1
       }
    ]

    Tuesday, July 17, 2018 9:41 PM

All replies

  • User1992938117 posted

    Use below code:

    private List<string> GetHallNames()
            {
                var resourceNames = new List<string>();
                dynamic storydata = JsonConvert.DeserializeObject(File.ReadAllText(@Server.MapPath(@"~/App_Data/Data.json")));
                foreach (var d in storydata)
                {
                    resourceNames.Add(d.hall_name.ToString());
                }
    
                return resourceNames;
            }

    Wednesday, July 18, 2018 6:14 AM
  • User1204533129 posted

    try this :

    var Result = //somerecords ;

    List<string> tList = new List<string>();

    for(int i = 0; i<Result.Length:i++){

    t.add(Result[i].hall_name.ToString());

    }

    return tList;

    Thursday, October 4, 2018 8:00 PM