Как изучить все свойства и и его значение во вложенном объекте json в C #? - PullRequest
0 голосов
/ 30 октября 2018
{
    "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);

Но это дает мне только данные первого ребенка, только вот так

Главная

Root

вложенная

subfolder_1

фотографии

photos_of_camera

Январь

Февраль

Март

Апрель

Май

имя

дата

место

Деталь фото

но я хочу все свойства и их значение, так что кто-нибудь может мне помочь ??

1 Ответ

0 голосов
/ 31 октября 2018

Если цель состоит в том, чтобы просто отобразить все свойства и их значения, вы можете рассмотреть следующий рекурсивный подход, поэтому он может стать для вас отправной точкой.

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());
}
...