如何使用 C# 反序列化没有 属性 名称的 json 文档?

How to deserialize a json document that does not have property names using C#?

Json 响应是这样的:

[
    [
        1597276800000,
        "16.46340000",
        "18.34880000",
        "15.91750000",
        "17.18830000",
        "30941693.96000000",
        1597363199999,
        "527277033.75007300",
        681520,
        "15434492.74000000",
        "263241680.21583200",
        "0"
    ],
    [
        1597363200000,
        "17.17280000",
        "17.59980000",
        "16.30000000",
        "16.86580000",
        "11130678.41000000",
        1597449599999,
        "188347963.49490200",
        244865,
        "5421845.98000000",
        "91775690.92871400",
        "0"
    ]
]

我知道这些属性的标签,但它们不在 json 文档中。 这是响应在 json 查看器中的显示方式。 当我用 json2csharp.com 转换时,我得到这些:

    public class Root    {
        public List<List<object>> MyArray { get; set; } 
    }

and 

Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse); 

编译器抱怨:

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<T> 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.

列表列表或列表数组应该可以工作,但我不断收到相同的错误消息。

有什么想法吗?

谢谢

您没有带对象的 json,只是包含另一个 list of stringslist

你应该像这样反序列化,

var obj = JsonConvert.DeserializeObject<List<List<string>>>(myJsonResponse);

选项 1

您可以使用动态代替对象。它的属性和行为在运行时解决。

然后您可以执行强制转换 - 我只是在使用像这样的低级不安全上下文之前这样做过:

public class Root
{
  public dynamic[][] MyArray { get; set; }
}
// ...
fixed (byte* ptr = root.MyArray)
{
  // conversion logic
}

选项 2

编写您自己的 JSON 解析器不会花太长时间,特别是如果它不是通用的。

最简单的方法是构建一个 Json 能够操作对象的转换器。如果您 Json 响应的数据始终具有相同的结构,我认为它会更容易操作。

这是我为自定义 Json转换器整理的一些快速代码:

public class DataObjectConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(DataObject);
    }
    public override bool CanRead => true;
    public override bool CanWrite => false;

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;
        var array = JArray.Load(reader);
        var dataObj = (existingValue as DataObject ?? new DataObject());
        dataObj.PropertyA = (long)array.ElementAtOrDefault(0);
        dataObj.PropertyB = (string)array.ElementAtOrDefault(1);
        dataObj.PropertyC = (string)array.ElementAtOrDefault(2);
        dataObj.PropertyD = (string)array.ElementAtOrDefault(3);
        dataObj.PropertyE = (string)array.ElementAtOrDefault(4);
        dataObj.PropertyF = (string)array.ElementAtOrDefault(5);
        dataObj.PropertyG = array.ElementAtOrDefault(6).ToObject<long>();
        dataObj.PropertyH = (string)array.ElementAtOrDefault(7);
        dataObj.PropertyI = (int)array.ElementAtOrDefault(8);
        dataObj.PropertyJ = (string)array.ElementAtOrDefault(9);
        dataObj.PropertyK = (string)array.ElementAtOrDefault(10);
        dataObj.PropertyL = (string)array.ElementAtOrDefault(11);
        return dataObj;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

我创建了一个虚拟对象,以便能够提取每个字段的实际类型。

class DataObject
{
    public long PropertyA { get; set; }
    public string PropertyB { get; set; }
    public string PropertyC { get; set; }
    public string PropertyD { get; set; }
    public string PropertyE { get; set; }
    public string PropertyF { get; set; }
    public long PropertyG { get; set; }
    public string PropertyH { get; set; }
    public long PropertyI { get; set; }
    public string PropertyJ { get; set; }
    public string PropertyK { get; set; }
    public string PropertyL { get; set; }
}

然后您可以按如下方式使用此自定义Json转换器:

    //Extract Typed List
    var typedData = JsonConvert.DeserializeObject<List<DataObject>>(json, new DataObjectConverter());