如何在C#中的嵌套json对象中探索所有属性及其值?

用户名
{
    "Main": 
    {
       "subfolder" : 
        {        
           "photos" : 
            {
              "January" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "February" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "March" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "April" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "May" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo"
                }     
            },
            "photos_of_camera" : 
            {
              "January" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "February" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "March" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "April" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "May" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo"
                }     
            }
        },
         "subfolder_1" : 
        {        
           "photos" : 
            {
              "January" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "February" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "March" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "April" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "May" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo"
                }     
            },
            "photos_of_camera" : 
            {
              "January" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "February" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "March" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "April" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "May" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo"
                }     
            }
        }
    },
    "Root": 
    {
       "subfolder" : 
        {        
           "photos" : 
            {
              "January" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "February" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "March" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "April" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "May" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo"
                }     
            },
            "photos_of_camera" : 
            {
              "January" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "February" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "March" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "April" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "May" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo"
                }     
            }
        },
         "subfolder_1" : 
        {        
           "photos" : 
            {
              "January" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "February" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "March" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "April" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "May" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo"
                }     
            },
            "photos_of_camera" : 
            {
              "January" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "February" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "March" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "April" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo",
                    "location": "location detail"
                },
               "May" : 
                {
                    "name" : "Detail of photo",
                    "date": "id of photo"
                }     
            }
        }
    }
}

我有这样的数据。我想检索所有属性,如果有的话,它是值。我只能使用这样的代码使用Newtonsoft.json包检索第一个孩子

string a = File.ReadAllText(@"json file path");


List<string> read(string file)
{
    JObject jObject = JObject.Parse(file);
    foreach(JProperty i in jObject.Properties())
    {
        var name = i.Name;
        Console.WriteLine(bb);
    }
    var child = jObject.First.First;
    if(!st.HasValues)
    {
        Console.WriteLine(child.ToString());
        return null;
    }
    string cnr = Convert.ToString(child);
    return recursion(cnr);
}

recursion(a);

但这只给我第一个孩子的数据

主要

子文件夹

subfolder_1

相片

photos_of_camera

一月

二月

游行

四月

可能

名称

日期

位置

照片细节

但是我想要所有属性,它很有价值,所以有人可以帮助我吗?

扎瓦特

如果仅出于显示所有属性及其值的目的,则可以考虑使用以下递归方法,因此它可以作为您的起点。

string a = File.ReadAllText(@"json file path");
getPropertiesAndValues(a);

private static void getPropertiesAndValues(string json)
{
JObject jObject = JObject.Parse(json);
foreach (JProperty i in jObject.Properties())
{
    var name = i.Name;
    var value = i.Value;

    Console.Write($"Name: {name} \t\t");
    if (!i.Value.HasValues)
        Console.WriteLine($"Value: {i.Value}");
    Console.WriteLine();
    if (i.HasValues && i.Value.HasValues)
    getPropertiesAndValues(i.First.ToString());
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在JavaScript中为Closure Compiler注释嵌套对象,并使所有属性可选?

如何在javascript对象数组中设置所有对象的特定属性值(虚线)

如何遍历Typescript类中的所有属性及其值

jq:如何在带有数组的嵌套json树中添加对象,键/值

如何在对象数组中获取对象所有属性值的数组?

如何在Groovy中迭代嵌套地图的所有值

在包含对象中列出所有嵌套属性及其完整路径

如何检查类或json对象中的所有属性/值均为nil

如何在C#中获取嵌套的JSON值?

从C#中的动态对象中删除属性及其值

如何在PostgreSQL中显示所有对象及其注释?

在C#中以字符串形式获取所有属性及其值

如何在其他对象内的所有嵌套对象中动态添加新键/值?

如何获取JS对象中特定嵌套属性的所有值

从JSON数组中的嵌套对象中获取所有键及其值(不带jQuery)

在C#中记录对象的所有属性。如何记录内部对象的属性呢?

如何在LIST的所有对象中更改单独属性的值

如何在 Node 中刷新对象的所有属性

如何在 JSON 对象中更改对象的属性值

如何在不先复制所有值的情况下移动数组中对象的属性值?

如何在反应应用程序中按属性选择嵌套层次结构中的所有对象?

如何在嵌套对象数组中查找特定键的所有值?

如何在firebase数据库c#中获取嵌套在父节点下的所有子值

如何在 JavaScript 中动态获取嵌套对象属性的值?

如何在带有不同键的 json 对象中添加所有值

Javascript如何更新嵌套对象中的所有属性名称

打字稿从嵌套对象中获取属性的所有值

删除所有对象中的属性及其值

删除所有对象中的属性及其值