Determine if Json results is object or array

前端 未结 3 1968
借酒劲吻你
借酒劲吻你 2020-12-05 06:02

I am using .net web api to get json and return it to the front end for angular. The json can be either an object or an array. My code currently only works for the array not

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 06:44

    Using Json.NET, you could do this:

    string content = File.ReadAllText(path);
    var token = JToken.Parse(content);
    
    if (token is JArray)
    {
        IEnumerable phones = token.ToObject>();
    }
    else if (token is JObject)
    {
        Phone phone = token.ToObject();
    }
    

提交回复
热议问题