How to deserialize a JSON object where the structure of the object is unknown

Part of my code serializes the machine file paths in JSON in the following format. I'm struggling to take this JSON and put the file paths again. I am using Newtonsoft JSON lib; I find this excellent for creating JSON. As you can see, my JSON has nested objects.

At JSON I have:

{
  ".": {
    "proc": {
      "15": {
        "task": {
          "15": {
            "exe": {},
            "mounts": {
              "list_of_files": [
                "mounts.xml"
              ]
            },
            "mountinfo": {
              "list_of_files": [
                "mountinfo.xml"
              ]
            },
            "clear_refs": {
              "list_of_files": [
                "clear_ref.xml"
              ]
            }
          }
        }
      },
      "14": {
        "loginuid": {
          "list_of_files": [
            "loginuid.xml"
          ]
        },
        "sessionid": {
          "list_of_files": [
            "sessionid.xml"
          ]
        },
        "coredump_filter": {
          "list_of_files": [
            "coredump_filter.xml"
          ]
        },
        "io": {
          "list_of_files": [
            "io.xml"
          ]
        }
      }
    }
  }
}

The array that I want to generate from this.

string[] dirArray = {
"./proc/15/task/15/exe",
"./proc/15/task/15/mounts/mounts.xml",
"./proc/15/task/15/mountinfo/mountinfo.xml",
"./proc/15/task/15/clear_refs/clear_ref.xml",
"./proc/14/loginuid/loginuid.xml",
"./proc/14/sessionid/sessionid.xml",
"./proc/14/coredump_filter/coredump_filter.xml",
"./proc/14/io/io.xml"
}

My efforts so far - I have deserialised JSON into a dynamic variable, but I'm not sure how to handle two problems:

  • My JSON format is unknown, I don’t know how deep the objects are, how can I handle this?
  • How do I work with dynamic variables when they are defined at runtime?

EDIT

, JSON , , 12864. : Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'Newtonsoft.Json.Linq.JObject'.

fiddle, , .

+4
3

@user12864 , , , "" ( ). :

private static void AddToFileList(JObject jo, List<string> list, string prefix)
{
    foreach (var kvp in jo)
    {
        if (kvp.Key == "list_of_files")
        {
            foreach (string name in (JArray)kvp.Value)
            {
                list.Add(prefix + name);
            }
        }
        else
        {
            JObject dir = (JObject)kvp.Value;
            if (dir.Count == 0)
            {
                list.Add(prefix + kvp.Key);
            }
            else
            {
                AddToFileList(dir, list, prefix + kvp.Key + "/");
            }
        }
    }
}

:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
          ""."": {
            ""proc"": {
              ""15"": {
                ""task"": {
                  ""15"": {
                    ""exe"": {},
                    ""mounts"": {
                      ""list_of_files"": [
                        ""mounts.xml""
                      ]
                    },
                    ""mountinfo"": {
                      ""list_of_files"": [
                        ""mountinfo.xml""
                      ]
                    },
                    ""clear_refs"": {
                      ""list_of_files"": [
                        ""clear_ref.xml""
                      ]
                    }
                  }
                }
              },
              ""14"": {
                ""loginuid"": {
                  ""list_of_files"": [
                    ""loginuid.xml""
                  ]
                },
                ""sessionid"": {
                  ""list_of_files"": [
                    ""sessionid.xml""
                  ]
                },
                ""coredump_filter"": {
                  ""list_of_files"": [
                    ""coredump_filter.xml""
                  ]
                },
                ""io"": {
                  ""list_of_files"": [
                    ""io.xml""
                  ]
                }
              }
            }
          }
        }";

        JObject jo = JObject.Parse(json);
        foreach (string path in CreateFileList(jo))
        {
            Console.WriteLine(path);
        }
    }

    private static List<string> CreateFileList(JObject jo)
    {
        List<string> ret = new List<string>();
        AddToFileList(jo, ret, "");
        return ret;
    }

    private static void AddToFileList(JObject jo, List<string> list, string prefix)
    {
        foreach (var kvp in jo)
        {
            if (kvp.Key == "list_of_files")
            {
                foreach (string name in (JArray)kvp.Value)
                {
                    list.Add(prefix + name);
                }
            }
            else
            {
                JObject dir = (JObject)kvp.Value;
                if (dir.Count == 0)
                {
                    list.Add(prefix + kvp.Key);
                }
                else
                {
                    AddToFileList(dir, list, prefix + kvp.Key + "/");
                }
            }
        }
    }
}

:

./proc/15/task/15/exe
./proc/15/task/15/mounts/mounts.xml
./proc/15/task/15/mountinfo/mountinfo.xml
./proc/15/task/15/clear_refs/clear_ref.xml
./proc/14/loginuid/loginuid.xml
./proc/14/sessionid/sessionid.xml
./proc/14/coredump_filter/coredump_filter.xml
./proc/14/io/io.xml

Fiddle: https://dotnetfiddle.net/r8CkI2

+2

, ; JObject JObject.Parse CreateFileList. JSON .

    static List<string> CreateFileList(JObject j)
    {
        List<string> ret = new List<string>();
        AddToFileList(j, ret, "");
        return ret;
    }


    static void AddToFileList(JObject j, List<string> dest, string prefix)
    {
        if (prefix.Length != 0)
            prefix = prefix + '/';

        foreach (var kvp in j)
        {
            var jnext = (JObject)kvp.Value;
            if (kvp.Key == "file")
                dest.Add(prefix + (string)jnext["name"]);
            else
                AddToFileList(jnext, dest, prefix + kvp.Key);
        }
    }

https://dotnetfiddle.net/dQQ4tI

+2

Update:


, :

JavaScript , . .

, , , JavaScript, . , .

:

public class XmlPath
{
     public string Location { get; set; }
}

XmlPath . auto.

:

private List<XmlPath> AddXmlPath(List<string> location)
{
     List<XmlPath> content = new List<XmlPath>();
     foreach(string item in location)
          content.Add(new XmlPath() { Location = item });

     return content;           
}

, List<string> XmlPath.

:

private List<XmlPath> RemoveXmlPath(List<XmlPath> root, string location)
{
     root.Remove(new XmlPath() { Location = location });
     return root;
}

, , . , , . , .

/Deserialize JavaScript :

JavaScriptSerializer serializer = new JavaScriptSerializer();
var xmlPath = AddXmlPath(List<string> location);
var result = serializer.Serialize(xmlPath);
var deserialize = serializer.Deserialize(List<XmlPath>>(result);

:

foreach(XmlPath item in deserialize)
{
     // Exposed Model via 'item.Location'
}

. , , , . :

  • .
  • .
  • .

, .

0

All Articles