C#反序列化未标记的JSON数组 [英] c# Deserialize unlabelled JSON array

查看:116
本文介绍了C#反序列化未标记的JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反序列化具有特定结构的JSON片段,如下所示:

I am attempting to deserialize a piece of JSON with a specific structure like so:

{  
    "label1": "value1",  
    "label2": [  
        [  
            [  
                "concept_id_1",  
                "concept_1"  
            ],  
            score_1  
        ],  
        [  
            [  
                "concept_id_2",  
                "concept_2"  
            ],  
            score_2  
        ],  
        ……  
    ],  
    "label3": "value3",  
    "label4": "value4"  
}  

对于它的价值,分数是浮点数,其他所有都是字符串.在"label2"下返回的概念数是可变的.

For what it's worth, the scores are floats and everything else is a string. The number of returned concepts under "label2" is variable.

我正在尝试使用JSON.net反序列化它.我真正关心的唯一内容是标记为"label2"的数组的内部嵌套,但是数组内部缺少标签使我无处不在.

I'm attempting to deserialize it using JSON.net. The only content I actually care about is the inside nest of arrays labelled "label2", however the lack of labels inside the arrays is blocking me at every turn.

我尝试了多种方法,但迄今为止最成功的方法是:

I've tried a variety of approaches, but the most successful so far seems to be this:

public class Parsed_JSON {
    public string label1 { get; set; }
    public ICollection<Full_Result> label2 { get; set; }
    public string label3 { get; set; }
    public string label4 { get; set; }
}

public class Full_Result {
    public IList<string> values { get; set; }
    public float score { get; set; }
}

Parsed_JSON result = JsonConvert.DeserializeObject<Parsed_JSON>(JSON);

但是这失败并显示错误:

However this is failing with the error:

无法将当前JSON数组(例如[1,2,3])反序列化为类型'JSON_Parsing + Full_Result',因为该类型需要JSON对象(例如{"name":"value"})才能正确地反序列化. br> 要解决此错误,可以将JSON更改为JSON对象(例如{"name":"value"}),也可以将反序列化类型更改为数组,或者将实现集合接口的类型(例如ICollection,IList)更改为List,例如List从JSON数组反序列化.还可以将JsonArrayAttribute添加到该类型中,以强制其从JSON数组反序列化.

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'JSON_Parsing+Full_Result' 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.

最终,我将遍历label2的内容,以便可以像这样构建其中的DataTable:

Ultimately I'll be looking to iterate through the contents of label2 so that I can build a DataTable of them like so:

concept_id_1   concept_1   score_1  
concept_id_2   concept_2   score_2

如何反序列化此JSON?

How can I deserialize this JSON?

推荐答案

您可以使用此答案 C# JSON.NET-反序列化响应,该响应使用异常的数据结构 将您的JSON反序列化为现有的类型化数据模型.修改Full_Result如下:

[JsonConverter(typeof(ObjectToArrayConverter<Full_Result>))]
public class Full_Result 
{
    [JsonProperty(Order = 1)]
    public IList<string> values { get; set; }
    [JsonProperty(Order = 2)]
    public float score { get; set; }
}

您现在可以按如下方式反序列化:

And you will now be able to deserialize as follows:

Parsed_JSON result = JsonConvert.DeserializeObject<Parsed_JSON>(JSON);

注意:

  • ObjectToArrayConverter<T>通过将T的可序列化成员映射到数组来工作,该数组序列由数据合同属性 集.

  • ObjectToArrayConverter<T> works by mapping the serializable members of T to an array, where the array sequence is defined by the value of the JsonPropertyAttribute.Order attribute applied to each member. Data contract attributes with DataMemberAttribute.Order set could be used instead, if you prefer.

在您的JSON中,得分"值实际上不是数字:

In your JSON the "score" values are not actually numbers:

score_1
score_2

我假设这是问题中的错字,并且这些值实际上是 JSON标准.

I am assuming that this is a typo in the question and that these values are in fact well-formed numbers as defined by the JSON standard.

样本小提琴此处.

这篇关于C#反序列化未标记的JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆