How to get name of a JValue object

c#, json, json.net

Solution

As I've seen none of your code thus I'm spitballing, perhaps you're looking for `JToken.Parent` and `JProperty`?

// Assumes token is JToken, search for the owning JProperty
var parentProperty = token.Ancestors<JProperty>()
                          .FirstOrDefault();

// alternatively, if you know it'll be a property:
var parentProperty = ((JProperty)token.Parent);

var name = parentProperty.Name;

Problem

I am using Newtonsoft.Json for parsing Json text. For a reason I need name of JToken or Jvalue object. As per example if "ChoiceId":865 is JValue then I need to get "ChoiceId". But I am trying it for few hours now but could not figure out how. How can I get that name ? Thanks EXAMPLE: if this is the json file content: ``` {"ChoiceId":868,"Choice":"Post","Url":"/pst/goods"} ``` Then I can get ChoiceId value by using ``` JObject json = JObject.Parse(hole); JValue jvalue = (Jvalue)json["ChoiceId"]; string value = jvalue.Value; ``` But there is no property for getting the name ie."ChoiceId" . So my question is that how can I get it ?

Original source