How to get the name of a JValue object

I am using Newtonsoft.Json to parse Json text. For some reason, I need a JToken or Jvalue object name. For example, if "ChoiceId": 865 is a JValue, then I need to get "ChoiceId" . But I try this for several hours, but could not figure out how to do it. How can I get this name?

thanks

Example: if it is a json file:

{"ChoiceId":868,"Choice":"Post","Url":"/pst/goods"} 

Then I can get the ChoiceId value using

 JObject json = JObject.Parse(hole); JValue jvalue = (Jvalue)json["ChoiceId"]; string value = jvalue.Value; 

But there is no property to get the name ie. "ChoiceId". So my question is, how can I get it?

+4
source share
1 answer

Since I have not seen any of your code, so I'm spitballing, maybe you are 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; 
+6
source

All Articles