1

I have this json object.

{  
   "Apparel":[  
      "Athlete",
      73
   ],
   "Freeze":[  
      "Coin",
      19,
      "Fur",
      37,
      "Grade",
      72
   ],
   "Lobster":[  
      "Node",
      18
   ]
}

I read it in a string and convert it to a JObject like this:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
JObject myobj = JObject.Parse(mystr);

How should I go about fetching all the keys {"Apparel", "Freeze", "Lobster"} ?


If I do something like this:

    List<JToken> mylist = myobj.ToList<JToken>();
    int l = mylist.Count;

    for (int i = 0; i < l; i++)
    {
        Debug.WriteLine(mylist[i]);
    }

It prints:

    "Apparel": [
      "Athlete",
      73
    ]"Freeze": [
      "Coin",
      19,
      "Fur",
      37,
      "Grade",
      72
    ]
    "Lobster": [
      "Node",
      18
    ]

The values {"Apparel", "Freeze", "Lobster"} are not constant and could change. I know I can do some regex patches to get what I want from the string but I don't want to go down that road, unless it's a last resort. Please Help.

Adze
  • 155
  • 1
  • 1
  • 12

1 Answers1

1

You can use the jobject properties method,

IList<string> keys = parent.Properties().Select(p => p.Name).ToList();

taken from here

How can I get a list of keys from Json.NET?

Community
  • 1
  • 1