2
{
 "Date": "2016-12-15",
 "Data": {
   "A": 4.4023,
   "AB": 1.6403,
   "ABC": 2.3457
 }
}

how can i get my keys A,Ab,ABC into an array? just the keys not value.

sahi
  • 47
  • 1
  • 8

1 Answers1

2

You could install and use LINQ to JSON to query the properties:

var jsonString = @"{
     ""Date"": ""2016-12-15"",
     ""Data"": {
       ""A"": 4.4023,
       ""AB"": 1.6403,
       ""ABC"": 2.3457
     }
}";

var root = JToken.Parse(jsonString);

var properties = root
    // Select nested Data object
    .SelectTokens("Data")
    // Iterate through its children, return property names.
    .SelectMany(t => t.Children().OfType<JProperty>().Select(p => p.Name))
    .ToArray();

Console.WriteLine(String.Join(",", properties)); // Prints A,AB,ABC

Sample fiddle.

dbc
  • 104,963
  • 20
  • 228
  • 340