NewtonSoft JSON文件具有奇异对象和数组的组合

vddsk6oq  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(120)

使用NewtonSoft JSON -我们得到一个JSON文件,它可以由单一对象和数组组成(如下所示)。对于每个对象和每个数组,我们需要解析并输入到单独的数据表中。我们还可以为每个Singular Object和每个Array获取单独的JSON文件。这一点我们可以做到。但是-对于“一体化”-我们需要做的第一件事是检测第一个JSON读取-是否是一个“一体化JSON文件”。如果是,则需要将每个Singular Object解析为它的DT,并将每个Array及其值解析为DT。这是通过逐行步进并检测令牌类型来完成的吗?然后根据这个来表演看来应该有更快的办法。注意:我们不知道数组名称......它们将是动态的-这意味着不是每个合并JSON文件都将包含每个模式的所有数组。有些可能被排除在外。
寻找将JSON文件解析合并到DT的最佳和最有效的方法

JSON示例:

{
  "$schema": "https://abc.def.bay/schema-v1-0-0.json",
  "THISID": "2023",
  "THIS_status_date": "2023-03-30",
  "Array01": [
    {
      "This_ID": "1",
      "title": "Proj",
      "level": 1,
      "type": "typeA",
      "That_ID": "1",
      "Person": "Smith, John",
      "where": "N",
      "when": "Exit 1",
      "why": "Because"
    },
    {
      "This_ID": "2",
      "title": "Proj",
      "level": 1,
      "type": "typeB",
      "That_ID": "2",
      "Person": "Jones, Kelley",
      "where": "N",
      "when": "Exit 2",
      "why": "Because"
    }
  ],
  "Array2": [
    {
      "This_ID": "1",
      "title": "Title A",
      "level": 1,
      "where": "N",
      "why": "Because"
    },
    {
      "This_ID": "2",
      "title": "Title B",
      "level": 2,
      "BelongTo": "1",
      "where": "N",
      "why": "Because"
    }
  ],

字符串

jyztefdp

jyztefdp1#

我用不同的解析方法替换了我的答案

void Main()
{
    string json = "{\"$schema\":\"https://abc.def.bay/schema-v1-0-0.json\",\"THISID\":\"2023\",\"THIS_status_date\":\"2023-03-30\",\"Array01\":[{\"This_ID\":\"1\",\"title\":\"Proj\",\"level\":1,\"type\":\"typeA\",\"That_ID\":\"1\",\"Person\":\"Smith, John\",\"where\":\"N\",\"when\":\"Exit 1\",\"why\":\"Because\"},{\"This_ID\":\"2\",\"title\":\"Proj\",\"level\":1,\"type\":\"typeB\",\"That_ID\":\"2\",\"Person\":\"Jones, Kelley\",\"where\":\"N\",\"when\":\"Exit 2\",\"why\":\"Because\"}],\"Array2\":[{\"This_ID\":\"1\",\"title\":\"Title A\",\"level\":1,\"where\":\"N\",\"why\":\"Because\"},{\"This_ID\":\"2\",\"title\":\"Title B\",\"level\":2,\"BelongTo\":\"1\",\"where\":\"N\",\"why\":\"Because\"}]}";

    var jobj = JObject.Parse(json);
    foreach (JProperty content in jobj.Children().ToList())
    {
        // or can filter by type if(content.Value.GetType() == typeof(JArray))
        //if (content.Name.ToLower().StartsWith("arr"))
        if(content.Value.GetType() == typeof(JArray))
        {
            //can serialize to list
            var ja = JArray.FromObject(content.Value);
            ja.ToObject(typeof(List<Array01>)).Dump(content.Name);
            //now you can add to dictionary of (key,List<>) or do what you need

        }
    }
}

public class Array01
{
    public string This_ID { get; set; }
    public string title { get; set; }
    public int level { get; set; }
    public string type { get; set; }
    public string That_ID { get; set; }
    public string Person { get; set; }
    public string where { get; set; }
    public string when { get; set; }
    public string why { get; set; }
    public string BelongTo { get; set; }
}

字符串


的数据

相关问题