Asp.net http 客户端 json 响应

Asp.net http client json response

Asp.net C#。 这是我从外部网址获取的 json 字符串,我想将它存储在列表中,然后使用 gridview 显示它。 它给出了一个错误,即

无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[AMS_WEB_Form.Employee]',因为该类型需要一个 JSON 数组(例如 [1,2,3]) 以正确反序列化。

要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是整数之类的原始类型,也不是数组或列表之类的集合类型)可以从 JSON 对象反序列化。 JsonObjectAttribute 也可以添加到类型以强制它从 JSON 对象反序列化。

路径 'employees',第 1 行,位置 13。

请帮忙,

我正在使用的

class,..

public class Employee
{
    public int employee_id { get; set; }
    public string employee_name { get; set; }
}


public class RootObject
{
    public List<Employee> employees { get; set; }
}

这就是我正在做的....并在 PageLoads 视图事件中调用此任务。

async Task RunAsyncGetDataFromSource()
{
  using( var client = new HttpClient() )
  {               
     //HTTP get
     HttpResponseMessage response = await client.GetAsync("http://192.168.88.598:6598/employees");
     response.EnsureSuccessStatusCode();

     if( response.IsSuccessStatusCode )
     {
       var jsonString =  response.Content.ReadAsStringAsync().Result;
       var data = JsonConvert.DeserializeObject<List<Employee>>(jsonString);

       GridExternalUsersData.DataSource = data;
       GridExternalUsersData.DataBind();

       Response.Write("<script>alert('Data loaded successfully');</script>");
    }
  }           
}

我的 JSON WebAPI 输出采用这种格式。

"{\"employees\":[{\"employee_id\":1,\"employee_name\":\"Dalton Schimmel\"},{\"employee_id\":2, \"employee_name\":\"Caitlyn O'Kon\"},{\"employee_id\":3,\"employee_name\":\"Timmothy Bartell\"},{\"employee_id\":4, \"employee_name\":\"Mauricio Lind\"},{\"employee_id\":5,\"employee_name\":\"Mr. Imani Pfannerstill\"},{\"employee_id\":6, \"employee_name\":\"Mrs. Reagan Wehner\"},{\"employee_id\":7,\"employee_name\":\"Citlalli McCullough II\"},{\"employee_id\":8, \"employee_name\":\"Mrs. Fabian Leffler\"},{\"employee_id\":9,\"employee_name\":\"Blanca Langosh\"},{\"employee_id\":10, \"employee_name\":\"Kennith Watsica\"},{\"employee_id\":11,\"employee_name\":\"Fermin Leannon\"},{\"employee_id\":12, \"employee_name\":\"Estelle Windler\"},{\"employee_id\":13,\"employee_name\":\"Kurt Skiles\"},{\"employee_id\":14, \"employee_name\":\"Sienna Medhurst\"},{\"employee_id\":15,\"employee_name\":\"Orlando Bednar DVM\"},{\"employee_id\":16, \"employee_name\":\"Harley Leffler\"},{\"employee_id\":17,\"employee_name\":\"Nathaniel Crooks\"},{\"employee_id\":18, \"employee_name\":\"Hortense Jerde IV\"},{\"employee_id\":19,\"employee_name\":\"Coty Mills\"},{\"employee_id\":20, \"employee_name\":\"Savannah Grimes\"},{\"employee_id\":21,\"employee_name\":\"Ms. Darrick Moore\"},{\"employee_id\":22, \"employee_name\":\"Meta Hermann MD\"},{\"employee_id\":23,\"employee_name\":\"Mr. Ellsworth Sauer\"},{\"employee_id\":24, \"employee_name\":\"Salvatore Marks\"},{\"employee_id\":25,\"employee_name\":\"Rory Bahringer I\"},{\"employee_id\":26, \"employee_name\":\"Rory Robel Sr.\"},{\"employee_id\":27,\"employee_name\":\"Katheryn Erdman\"},{\"employee_id\":28, \"employee_name\":\"Lexie Deckow Jr.\"},{\"employee_id\":29,\"employee_name\":\"Erling Jenkins\"},{\"employee_id\":30, \"employee_name\":\"Chris Gusikowski\"},{\"employee_id\":31,\"employee_name\":\"Kelli Mraz\"},{\"employee_id\":32, \"employee_name\":\"Gregg Hartmann\"},{\"employee_id\":33,\"employee_name\":\"Darrel Olson\"},{\"employee_id\":34, \"employee_name\":\ "Roxane Nicolas\"},{\"employee_id\":35,\"employee_name\":\"Julian Hintz\"},{\"employee_id\":36,\"employee_name\":\ "Elian White\"},{\"employee_id\":37,\"employee_name\":\"Annabel Nader\"},{\"employee_id\":38,\"employee_name\":\ "Brycen Vandervort V\"},{\"employee_id\":39,\"employee_name\":\"Ms. Gerald Roob\"},{\"employee_id\":40,\"employee_name\":\ "Dr. Lesley Feil\"},{\"employee_id\":41,\"employee_name\":\"Owen Schmidt\"},{\"employee_id\":42,\"employee_name\":\ "Kayden Effertz\"},{\"employee_id\":43,\"employee_name\":\"Muhammad Funk\"},{\"employee_id\":44,\"employee_name\":\ "Justyn Klein\"},{\"employee_id\":45,\"employee_name\":\"Ms. Jennie Pfannerstill\"},{\"employee_id\":46,\"employee_name\":\ "Ms. Celestine Konopelski\"},{\"employee_id\":47,\"employee_name\":\"Kayden Jones\"},{\"employee_id\":48,\"employee_name\":\ "Dr. Anabelle Emmerich\"},{\"employee_id\":49,\"employee_name\":\"Mrs. Isabell Kemmer\"},{\"employee_id\":50,\"employee_name\":\ "Jovani Kiehn\"},{\"employee_id\":51,\"employee_name\":\"Otis Schiller\"},{\"employee_id\":52,\"employee_name\":\ "Mrs. Aron Daniel\"},{\"employee_id\":53,\"employee_name\":\"Lurline Mayer\"},{\"employee_id\":54,\"employee_name\":\ "Tiana Wisozk\"},{\"employee_id\":55,\"employee_name\":\"Laila Predovic\"},{\"employee_id\":56,\"employee_name\":\ "Moises Carter\"},{\"employee_id\":57,\"employee_name\":\"Lizzie Yost\"},{\"employee_id\":58,\"employee_name\":\ "Zoe Kirlin\"},{\"employee_id\":59,\"employee_name\":\"Kirk Romaguera\"},{\"employee_id\":60,\"employee_name\":\ "Travis Orn\"},{\"employee_id\":61,\"employee_name\":\"Dr. Okey Mertz\"},{\"employee_id\":62,\"employee_name\":\ "Kristoffer Gerhold\"},{\"employee_id\":63,\"employee_name\":\"Unique Beier\"},{\"employee_id\":64,\"employee_name\":\ "Mrs. Halle Runolfsson\"},{\"employee_id\":65,\"employee_name\":\"Ariane Kris\"},{\"employee_id\":66,\"employee_name\":\ "Irma Witting III\"},{\"employee_id\":67,\"employee_name\":\"Rupert Streich PhD\"},{ \"employee_id\":68,\"employee_name\":\"Dahlia Kemmer Sr.\"},{\"employee_id\":69,\"employee_name\":\"Ms. George Thompson\"},{ \"employee_id\":70,\"employee_name\":\"Emie Green IV\"},{\"employee_id\":71,\"employee_name\":\"Miss Ibrahim Botsford\"},{ \"employee_id\":72,\"employee_name\":\"Felton Waters IV\"},{\"employee_id\":73,\"employee_name\":\"Letitia Thiel\"},{ \"employee_id\":74,\"employee_name\":\"Marquis Corwin\"},{\"employee_id\":75,\"employee_name\":\"Destini Rogahn V\"},{ \"employee_id\":76,\"employee_name\":\"Carleton Steuber\"},{\"employee_id\":77,\"employee_name\":\"Amiya Von PhD\"},{ \"employee_id\":78,\"employee_name\":\"Adele Kunde\"},{\"employee_id\":79,\"employee_name\":\"Miss Elijah Glover\"},{ \"employee_id\":80,\"employee_name\":\"Joelle Thiel\"},{\"employee_id\":81,\"employee_name\":\"Eino Bogan\"},{ \"employee_id\":82,\"employee_name\":\"Ms. Jake Kerluke\"},{\"employee_id\":83,\"employee_name\":\"Ethyl Waters\"},{ \"employee_id\":84,\"employee_name\":\"Vanessa Lindgren\"},{\"employee_id\":85,\"employee_name\":\"Valentin Cummerata\"},{ \"employee_id\":86,\"employee_name\":\"Dr. Halle Fadel\"},{\"employee_id\":87,\"employee_name\":\"Savanah King\"},{ \"employee_id\":88,\"employee_name\":\"Christop Effertz\"},{\"employee_id\":89,\"employee_name\":\"Cristopher Jacobs\"},{ \"employee_id\":90,\"employee_name\":\"Prudence Cruickshank II\"},{\"employee_id\":91,\"employee_name\":\"Karlee Herman\"},{ \"employee_id\":92,\"employee_name\":\"Rosamond Rau\"},{\"employee_id\":93,\"employee_name\":\"Marina Fritsch\"},{ \"employee_id\":94,\"employee_name\":\"Stefanie Johns\"},{\"employee_id\":95,\"employee_name\":\"Tevin Murazik\"},{ \"employee_id\":96,\"employee_name\":\"Audra Bins\"},{\"employee_id\":97,\"employee_name\":\"Melba Larson\"},{ \"employee_id\":98,\"employee_name\":\"Asa Schaefer\"},{\"employee_id\":99,\"employee_name\":\"Spencer Jenkins II\"},{ \"employee_id\":100,\"employee_name\":\"Elda Brakus\"}]}" stri吴

您需要将 JsonConvert 更改为:
var data = JsonConvert.DeserializeObject<RootObject>(jsonString);

或者,您可以从 JSON

中删除 employees 部分