展平没有名称属性的JSON

Don*_*Don 2 c# json

{
    "build": 44396,
    "files": ["00005DC8F14C92FFA13E7FDF1C9C35E4684F8B7A", [
        ["file1.zip", 462485959, 462485959, 2, 0, 883, true, 266716, 1734, 992, 558, 0],
        ["file1.doc", 521042, 521042, 2, 0, 883, true, 266716, 1734, 992, 558, 0]
    ], "0001194B90612DFB5E8D363249719FB62E221430", [
        ["file2.iso", 501163544, 501163544, 2, 0, 956, true, 194777, 2573, 0, 0, 0]
    ], "0002B5245B0897BEA7D7F426E104B6D24FF368DE", [
        ["file3.mp4", 284564707, 284564707, 2, 0, 543, true, 205165, 1387, 853, 480, 0]
    ]]
}
Run Code Online (Sandbox Code Playgroud)

我喜欢将上面的json展平为ID,fileName,fileSize IE

00005DC8F14C92FFA13E7FDF1C9C35E4684F8B7A    file1.zip   462485959
00005DC8F14C92FFA13E7FDF1C9C35E4684F8B7A    file1.doc   521042
0001194B90612DFB5E8D363249719FB62E221430    file2.iso   501163544
0002B5245B0897BEA7D7F426E104B6D24FF368DE    file3.mp4   284564707
Run Code Online (Sandbox Code Playgroud)

如您所见,大多数属性没有名称.文件数组还将id与文件对象交替.

我尝试使用类,但得到错误:"无法反序列化当前的JSON数组{"无法将当前的JSON数组(例如[1,2,3])反序列化为类型'Rename.frmMain + jsonFiles',因为该类型需要JSON对象(例如{\"name \":\"value \"})以正确反序列化.要修复此错误,请将JSON更改为JSON对象(例如{\"name \":\"value \"})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollection,IList)像可以从JSON数组反序列化的List.JsonArrayAttribute也可以添加到类型中以强制它从JSON数组反序列化.\ r \nPath'files',第1行,第25位."} Newtonsoft.Json.JsonSerializationException

public class jsonDetails
{
  public int build { get; set; }
  public object files { get; set; }
}

public class jsonFiles
{
  public string hash { get; set; }
  public string[][] files { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我尝试过使用ExpandoObjects没有运气.

我看过linq给JSon.

我在网上看到的大多数例子都需要属性名称.

我们如何压扁上面的JSON?

任何帮助,将不胜感激.

spe*_*der 5

您可以JObject直接使用"无模式"并使用,然后使用一些LINQ来解决问题:

var jsonStr = @"{
    ""build"": 44396,
    ""files"": [""00005DC8F14C92FFA13E7FDF1C9C35E4684F8B7A"", [

        [""file1.zip"", 462485959, 462485959, 2, 0, 883, true, 266716, 1734, 992, 558, 0],
        [""file1.doc"", 521042, 521042, 2, 0, 883, true, 266716, 1734, 992, 558, 0]
    ], ""0001194B90612DFB5E8D363249719FB62E221430"", [

        [""file2.iso"", 501163544, 501163544, 2, 0, 956, true, 194777, 2573, 0, 0, 0]
    ], ""0002B5245B0897BEA7D7F426E104B6D24FF368DE"", [

        [""file3.mp4"", 284564707, 284564707, 2, 0, 543, true, 205165, 1387, 853, 480, 0]
    ]]
}";

var json = JObject.Parse(jsonStr);
var filesArr = json["files"];
var result = filesArr
    .Select((a, i) => new { a, i })
    .GroupBy(x => x.i / 2)
    .Select(g => g.ToList())
    .Select(g => new { id = g[0].a, vals = g[1].a })
    .SelectMany(x => x.vals.Select(v => new { x.id, val = v }))
    .Select(x => new { id = x.id.Value<string>(), 
                       filename = x.val[0].Value<string>(), 
                       size = x.val[1].Value<long>() });
Run Code Online (Sandbox Code Playgroud)