1 回答

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊
根據(jù)您發(fā)布的序列化 json,下面應(yīng)該是要反序列化到的正確模型。我相信您了解當(dāng)前模型與您擁有的 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 關(guān)注
- 127 瀏覽
添加回答
舉報(bào)