i'm working with Microsoft Json library. (DataContractJsonSerializer)
i thought it was a nice library but it caught very hard problem for me. first, please see the screenshot please.
the json file was clear. because i test it some online json parse sites like here
a problem is it'cant deserialize json rightly. it was cracked.
i. terms[0].labels=null. but as web parser showed, it contains 'labels' structure. ii. terms1.labels!=null. but as web parser showed, it contains nothing. (terms1 doesn't 'labels' structure, instead terms[0]
a problem is not only that.
the 'primaries' contains number of two array originally, but microsoft json returns just one member only.
even, terms1.labels[0].text have a string value "[US]" but, a string "[US]" is not exist in entire json string!
here is my structure for json.
[DataContract]
public struct GDicJson
{
[DataMember] public string query;
[DataMember] public string sourceLanguage;
[DataMember] public string targetLanguage;
[DataMember] public List<GDicResultContent> primaries;
[DataMember] public List<GDicResultContent> webDefinitions;
[DataContract]
public struct GDicResultContent
{
[DataMember] public string type;
[DataMember] public List<GDicResultTerm> terms;
[DataMember] public List<GDicResultEntry> entries;
}
[DataContract]
public struct GDicResultTerm
{
[DataMember] public string type;
[DataMember] public string text;
[DataMember] public string language;
[DataMember] public List<GDicResultLabel> labels;
}
[DataContract]
public struct GDicResultLabel
{
[DataMember] public string text;
[DataMember] public string title;
}
[DataContract]
public struct GDicResultEntry
{
[DataMember] public string type;
[DataMember] public List<GDicResultTerm> terms;
[Data开发者_运维知识库Member] public List<GDicResultEntry> entries;
}
}
i like to my source json file but StackOverflow doesn't provide upload file. i'm sorry for that. anyway, help me anybody.
if you want, i'll send a json file to your email with my source code.
But still as suggested by @StriplingWarrior, Json.NET has some issues too. I had a correct Json string containing basically just arrays within squared brackets and Json.NET was unable to parse it correctly and was returning nulls. I used fastJSON instead and all problems were solved. Plus is a way faster library than Json.NET. Give it a look at http://www.codeproject.com/KB/IP/fastJSON.aspx it also pretty easy to implement.
This is just a guess but it could be that because you're using structs, it's not updating instance's field upon deserialization. Can you change these to be classes instead of structs and try it out?
精彩评论