我试图反序列化,我从LinkedIn的Javascript API得到一个JSON对象:反序列化LinkedIn个人资料Json?
{"_key":"~","educations":{"_total":2,"values":[{"degree":"Bachelor of Science (BSc)","endDate":{"year":2004},
"fieldOfStudy":"Computer Software Engineering","id":134450447,"schoolName":"Bristol University","
startDate":{"year":2009}},{"id":143651018,"schoolName":"University of Kingston"}]},"emailAddress":
"[email protected]","firstName":"Robert","lastName":"Matthews"}
我写了一个自定义类来存储这些值,并使用反序列化它们Json.NET:
[Serializable]
public class LinkedInUserData {
[JsonProperty(PropertyName = "emailAddress")]
public string EmailAddress { get; set; }
// other properties cut out for simplicity
[JsonProperty(PropertyName = "educations")]
public Educations Educations { get; set; }
}
[Serializable]
public class Educations {
[JsonProperty(PropertyName = "_total")]
public string Total { get; set; }
[JsonProperty(PropertyName = "values")]
public Values Values { get; set; }
}
[Serializable]
public class Values { // cut down for simplicity
[JsonProperty(PropertyName = "degree")]
public string Degree { get; set; }
}
LinkedInUserData linkedData = JsonConvert.DeserializeObject<LinkedInUserData>(profile);
我能够在单个对象(无阵列等)转换没有任何问题,但我被陷在值对象的教育,并出现以下错误信息:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Data.Values' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'educations.values', line 1, position 47.
我已经尝试将Educations中的Values对象更改为字符串数组,但没有运气。有没有什么办法可以将Json中的值反序列化到我的自定义类中,还是只能沿着字符串数组获取某些内容?
编辑 - 清单产品(值)以下错误:
Error reading string. Unexpected token: StartObject. Path 'educations.values[0].endDate', line 1, position 96.
编辑2 - 好吧,我现在就买下,列表(值)所做的工作,然后将其绊倒在StartDate和EndDate上,因为它们本身就是对象,而且我将它们设置为strings
。我很努力去理解Json
字符串,但Link2CSharp
帮助我解决了这个问题。
你有没有试过将属性'Values'的类型改成'List'或'Values []'? –
Leri
我很确定我已经尝试过公共小巴,但我还是要仔细检查! – DevDave