简体   繁体   English

C#反序列化未标记的JSON数组

[英]c# Deserialize unlabelled JSON array

I am attempting to deserialize a piece of JSON with a specific structure like so: 我试图反序列化具有特定结构的JSON片段,如下所示:

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

For what it's worth, the scores are floats and everything else is a string. 就其价值而言,分数是浮点数,其他所有东西都是字符串。 The number of returned concepts under "label2" is variable. 在“ label2”下返回的概念的数量是可变的。

I'm attempting to deserialize it using JSON.net. 我正在尝试使用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. 我真正关心的唯一内容是标记为“ label2”的数组的内部嵌套,但是数组内部缺少标签使我无处不在。

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: 但是,此操作失败并显示以下错误:

Cannot deserialize the current JSON array (eg [1,2,3]) into type 'JSON_Parsing+Full_Result' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. 无法将当前JSON数组(例如[1,2,3])反序列化为类型'JSON_Parsing + Full_Result',因为该类型需要JSON对象(例如{“ name”:“ value”})才能正确反序列化。
To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array. 要解决此错误,可以将JSON更改为JSON对象(例如{“ name”:“ value”}),也可以将反序列化类型更改为数组,或者将实现集合接口的类型(例如ICollection,IList)更改为List,例如List从JSON数组反序列化。 JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. 还可以将JsonArrayAttribute添加到类型中,以强制其从JSON数组反序列化。

Ultimately I'll be looking to iterate through the contents of label2 so that I can build a DataTable of them like so: 最终,我将寻求遍历label2的内容,以便可以像这样构建其中的DataTable:

concept_id_1   concept_1   score_1  
concept_id_2   concept_2   score_2

How can I deserialize this JSON? 如何反序列化此JSON?

You can use the custom JsonConverter ObjectToArrayConverter<Full_Result> from this answer to C# JSON.NET - Deserialize response that uses an unusual data structure to deserialize your JSON into your existing typed data model. 您可以从此答案使用自定义JsonConverter ObjectToArrayConverter<Full_Result>C#JSON.NET-反序列化响应,该响应使用异常的数据结构将JSON反序列化为现有的类型化数据模型。 Modify Full_Result as follows: 修改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);

Notes: 笔记:

  • 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. ObjectToArrayConverter<T>通过将T可序列化成员映射到一个数组来工作,该数组序列由应用于每个成员的JsonPropertyAttribute.Order属性的值定义。 Data contract attributes with DataMemberAttribute.Order set could be used instead, if you prefer. 如果愿意,可以使用设置了DataMemberAttribute.Order 数据协定属性

  • In your JSON the "score" values are not actually numbers: 在您的JSON中,“得分”值实际上不是数字:

     score_1 score_2 

    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 . 我假设这是问题中的错别字,并且这些值实际上是JSON标准定义的格式正确的数字。

Sample fiddle here . 样品在这里摆弄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM