1 回答
TA贡献1794条经验 获得超7个赞
根据您发布的序列化 json,下面应该是要反序列化到的正确模型。我相信您了解当前模型与您拥有的 json 不兼容
public class Name
{
public string first_name { get; set; }
public string last_name { get; set; }
}
public class Email
{
public string value { get; set; }
}
public class DaPOCO
{
public Name name { get; set; }
public List<Email> emails { get; set; }
}
你可以试试下面的东西
using System;
using System.Linq;
using Newtonsoft.Json.Linq;
public class Program
{
public static void Main()
{
string responseString = @"{ name: { first_name: 'Foo', last_name: 'Bar' }, emails: [ {value: 'foo@bar.com' } ] }";
JObject jo = JObject.Parse(responseString);
JObject obj = (jo["emails"] as JArray).FirstOrDefault(x => !string.IsNullOrEmpty(x.Value<string>("value"))) as JObject;
DaPOCO poco = new DaPOCO
{
FirstName = ((jo["name"] as JObject)["first_name"]).ToString(),
Email = obj["value"].ToString(),
};
Console.WriteLine(poco.FirstName + "\t" + poco.Email);
}
}
public class DaPOCO
{
public string FirstName { get; set; }
public string Email { get; set;}
}
- 1 回答
- 0 关注
- 91 浏览
添加回答
举报