Reputation: 13
I have a JSON string "{name :\"daijiepei\"}"
. I'm using a JObject to deserialize it:
JObject json = JObject.Parse(str);
string value = obj["name"];
So I can get the value, but I can't get the JSON key. How do I get the key for a JSON value?
Upvotes: 0
Views: 1457
Reputation: 69372
You can iterate through the Properties
method of JObject
and get the Key
property from there. Example code from the documentation.
JObject o = new JObject
{
{ "name1", "value1" },
{ "name2", "value2" }
};
foreach (JProperty property in o.Properties())
{
Console.WriteLine(property.Name + " - " + property.Value);
}
// name1 - value1
// name2 - value2
foreach (KeyValuePair<string, JToken> property in o)
{
Console.WriteLine(property.Key + " - " + property.Value);
}
// name1 - value1
// name2 - value2
Upvotes: 1