是微软的Json(System.Runtime.Serialization.Json)错误吗?

时间:2011-07-26 16:24:26

标签: c# json

我正在使用Microsoft Json库。 (DataContractJsonSerializer

我认为这是一个不错的图书馆,但它给我带来了非常难的问题。 首先,请看截图。

enter image description here

json文件很清楚。因为我测试了一些在线json解析网站,如here

一个问题是它无法正确地反序列化json。它破裂了。

I 即可。术语[0] = .labels空。但正如网络解析器所示,它包含“标签”结构。 的即可。条款1。标签!= NULL。但正如网络解析器所示,它什么都没有。 (术语1不是'标签'结构,而是术语[0]

不仅仅是问题。

'primaries'最初包含两个数组的数量,但是microsoft json只返回一个成员。

甚至,术语1。labels [0] .text有一个字符串值“[US]”但是,整个json字符串中不存在字符串“[US]”!

这是我的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;
        [DataMember] public List<GDicResultEntry> entries;
    }
}

我喜欢我的源json文件,但StackOverflow不提供上传文件。对不起,我很抱歉。 无论如何,帮助我任何人。

如果您愿意,我会使用我的源代码将json文件发送到您的电子邮件。

2 个答案:

答案 0 :(得分:1)

但仍然如@StriplingWarrior所建议,Json.NET也存在一些问题。我有一个正确的Json字符串,基本上只包含方括号内的数组,Json.NET无法正确解析它并返回空值。我使用了fastJSON,所有问题都解决了。 Plus是比Json.NET更快的库。看看http://www.codeproject.com/KB/IP/fastJSON.aspx它也很容易实现。

答案 1 :(得分:0)

这只是一个猜测,但可能是因为你正在使用结构,它不会在反序列化时更新实例的字段。你能将这些改为类而不是结构并试试吗?