Json.net SelectToken with embedded "."

I have json that looks like this:

myjson = {"queries":{"F.SP": 27}}

therefore using

queryResults = JObject.Parse(jsonString)

I can do

firstToken = queryResults.SelectToken("queries") 

and return LinqJToken

{"F.SP": 27}

but i'm stuck because when i try

subToken = firstToken.SelectToken("F.SP")

I get nothing. I assume this is because JSON.net is looking for the "F" token with the subtoken "SP".

I also tried each of the following, to no avail

myToken = queryResults.SelectToken("queries.F.SP")     
myToken = queryResults.SelectToken("queries[0].F.SP")     

(queryResults.SelectToken ("queries [0]") returns nothing, fwiw)

Any ideas?

EDIT: I checked that the built-in "." This is problem; if i change the original json to

{"queries":{"FSP": 27}}

I can do

queryResults.SelectToken("queries").SelectToken("FSP")

no problems

+5
source share
4 answers

This will not return the token itself, but will return a value (this is probably what you are looking for anyway) ...

queryResults.SelectToken("queries").Value<int>("F.SP");
+3
source

, SelectToken, ?

subToken = queryResults["F.SP"];
+1

JSON:

{"queries":{"F.SP": 27}}

You can use SelectToken escaping:

queryResults.SelectToken("queries").SelectToken("['F.SP']")

or

queryResults.SelectToken("queries.['F.SP']")

Here are some more escaped examples: http://www.newtonsoft.com/json/help/html/QueryJsonSelectTokenEscaped.htm

+1
source
JObject obj = JObject.Parse(jsonstring);
var fsp = obj["queries"].First().First();

Not the most elegant, but it gets value.

0
source

All Articles