Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json.net SelectToken with embedded "."

Tags:

json.net

I have json that looks like:

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

so with

queryResults = JObject.Parse(jsonString)

I can do

firstToken = queryResults.SelectToken("queries") 

and get back the LinqJToken

{"F.SP": 27}

but I'm then stuck, because when I try

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

I get Nothing. I'm guessing this is because JSON.net is looking for a token "F" with subtoken "SP".

I've 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 have verified that the embedded "." is the problem; if I change the original json to

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

I can do

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

no problem

like image 488
davej Avatar asked Apr 25 '12 15:04

davej


1 Answers

If you have such names in JSON fields:

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

You may use SelectToken escaping:

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

or

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

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

like image 112
Anton Avatar answered Sep 21 '22 03:09

Anton