简体   繁体   中英

How can i convert an array in Json string to c# object?

I have a json return result like the following:

Json =

{ "Id":"12345", "FirstName":"Bob", "LastName":"Builder", "Links":[] }

Links can have a list of objects of type LinkService, i want to cast even if the array is empty , so in c# i get an empty array.

I do the following

      var token = JObject.Parse(Json);
      var Id = token.Value<string>("Id");
      var fname = token.Value<string>("FirstName");
      var lbname = token.Value<bool>("LastName");
      var links = JsonSerializer.Deserialize<List<LinkService>>(token.Value<Array>("Links"));

Issue is that it says cant convert System.Array to Newtonsoft.Json.JsonReader

您可以使用ToObject方法将 jToken 转换为对象:

var links = token["Links"].TObject<List<LinkService>>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM