2
{
    "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"
                }     
            }
        }
    }
}

I have data like this . I want to retrieve all property and it's value if it has. I able to retrieve only first child using Newtonsoft.json package using code like this

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);

But it gives me only first child's data only like this

Main

Root

subfolder

subfolder_1

photos

photos_of_camera

January

February

March

April

May

name

date

location

Detail of photo

but I want all properties and it's value so can anyone help me ??

Community
  • 1
  • 1
  • What exactly do you mean by "all properties"? Do you want all the values of `name`, `date` and `location` and/or something else? Please show us what your output should be. – Rui Jarimba Oct 30 '18 at 13:12
  • 2
    You could use `DescendantsAndSelf()` as shown e.g. at the beginning of [this answer](https://stackoverflow.com/a/38544179/344280) to [Picking Out Simple Properties from Hierarchical JSON](https://stackoverflow.com/q/38543353/344280). [`SelectTokens("..*")`](http://www.newtonsoft.com/json/help/html/QueryJsonSelectTokenJsonPath.htm) as shown in [this answer](https://stackoverflow.com/a/38464217/344280) to [How can I generate all possible LINQ strings of a json object for Json.net?](https://stackoverflow.com/q/38463735/344280) is another possibility. – dbc Oct 30 '18 at 13:12
  • @RuiJarimba , output like this 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 , all in one list –  Oct 30 '18 at 13:23
  • @Shaktishah I noticed your "date " properties have an extra space, this is not intentional is it? – Rui Jarimba Oct 30 '18 at 14:26
  • *but I want all properties and it's value so can anyone help me ??* -- can you please [edit] your question to indicate exactly what you want for non-primitive properties like `"subfolder"`? – dbc Oct 30 '18 at 16:29
  • @RuiJarimba , No it's "date", not extra space there –  Oct 31 '18 at 04:16
  • @dbc , all property means if we take this data as tree structure then "subfolder" is internal node so I want all nodes(like root,internal, leaf) in list . –  Oct 31 '18 at 04:20
  • with the help of this https://stackoverflow.com/questions/38622957/querying-json-nested-arrays-with-linq-json-net-c-sharp I am able to retrieve all property but it's value I am not able to retrieve , so help me please –  Oct 31 '18 at 09:20
  • @Shakti your json string has an extra space on the date property name – Rui Jarimba Oct 31 '18 at 09:44
  • @RuiJarimba It's corrected sir.... –  Oct 31 '18 at 10:32
  • @Shaktishah I've edited the json string for you :-) – Rui Jarimba Oct 31 '18 at 10:38

1 Answers1

1

If the purpose is just to display all the Properties and their values you may consider the following recursive approach, so it can be a starting point for you.

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());
}
Zhavat
  • 214
  • 1
  • 6