2012-11-29 44 views
0

我试图反序列化,我从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帮助我解决了这个问题。

+2

你有没有试过将属性'Values'的类型改成'List '或'Values []'? – Leri

+0

我很确定我已经尝试过公共小巴,但我还是要仔细检查! – DevDave

回答

3

我相信你需要根据json重新调整你的类。你可以试着用Json生成C#类,使用json2csharp(如果你想自动创建针对json的C#类,这是一个很好的资源)。以下是它给出的结构。你可以使用JSON.Net来获取对象。

public class EndDate 
{ 
    public int year { get; set; } 
} 

public class StartDate 
{ 
    public int year { get; set; } 
} 

public class Value 
{ 
    public string degree { get; set; } 
    public EndDate endDate { get; set; } 
    public string fieldOfStudy { get; set; } 
    public int id { get; set; } 
    public string schoolName { get; set; } 
    public StartDate __invalid_name__ 
startDate { get; set; } 
} 

public class Educations 
{ 
    public int _total { get; set; } 
    public List<Value> values { get; set; } 
} 

public class RootObject 
{ 
    public string _key { get; set; } 
    public Educations educations { get; set; } 
    public string emailAddress { get; set; } 
    public string firstName { get; set; } 
    public string lastName { get; set; } 
} 
+0

+1不错的资源。 – Leri

+0

谢谢哈比非常有帮助+1!你认为我可以重构我的当前代码并坚持使用Json.Net吗? – DevDave

+0

@Tyler,由于您在Value中遇到错误,因此您应该有一个列表或一组值来正确反序列化对象 – Habib