public class Field { public int id { get; set; } public string type { get; set; } public object value { get; set; } public string editedBy { get; set; } public List flags { get; set; } public List categories { get; set; } public string updated { get; set; } public string created { get; set; } public string uri { get; set; } public bool? isConnection { get; set; } } public class contact { public bool isConnection { get; set; } public int id { get; set; } public List fields { get; set; } public List categories { get; set; } public int error { get; set; } public int restoredId { get; set; } public string created { get; set; } public string updated { get; set; } public string uri { get; set; } } public class Contacts { public List contact { get; set; } public int count { get; set; } public int start { get; set; } public int total { get; set; } public string uri { get; set; } public bool cache { get; set; } } public class RootObject { public Contacts contacts { get; set; } }
和
JavaScriptSerializer serializer1 = new JavaScriptSerializer(); RootObject obje = serializer1.Deserialize(returnStr);
但它在obje给了我0值。
首先使用jsonlint确保您的Json处于有效格式
然后使用json2csharp在其上生成类
public class Field { public int id { get; set; } public string type { get; set; } public object value { get; set; } public string editedBy { get; set; } public List
使用Newtonsoft JSON将Json反序列化为对象,然后您可以只访问其属性值。
JsonConvert.DeserializeObject(string json);
json对象的类(在纠正一些语法错误后使用http://jsonutils.com/生成):
public class Field { public int id { get; set; } public string type { get; set; } public object value { get; set; } public string editedBy { get; set; } public IList flags { get; set; } public IList categories { get; set; } public DateTime updated { get; set; } public DateTime created { get; set; } } public class Contact { public bool isConnection { get; set; } public int id { get; set; } public IList fields { get; set; } } public class Contacts { public IList contact { get; set; } } public class Example { public Contacts contacts { get; set; } }
反序列化(您可能需要添加对System.Web.Extensions的引用):
System.Web.Script.Serialization.JavaScriptSerializer deSer = new System.Web.Script.Serialization.JavaScriptSerializer(); JSonPrintSettingsToXml.Input.Example deserializedJSON = deSer.Deserialize(yourJSON);
public class Contact { public bool isConnection { get; set; } public int id { get; set; } public List fields { get; set; } } public class Field { public int id { get; set; } public string type { get; set; } public object value { get; set; } public string editedBy { get; set; } public string[] flags { get; set; } public string[] categories { get; set; } public DateTime updated { get; set; } public DateTime created { get; set; } } public class Name { public string givenName { get; set; } public string middleName { get; set; } public string familyName { get; set; } public string prefix { get; set; } public string suffix { get; set; } public string givenNameSound { get; set; } public string familyNameSound { get; set; } }
var json = JObject.Parse(returnStr); var fields = (JArray)json["contacts"]["contact"][0]["fields"]; var givenName = fields[0]["value"]["givenName"]; var familyName = fields[0]["value"]["familyName"]; var email = fields[1]["value"];