问题
Further on with Newtonsoft.Json, Path returned multiple tokens,
For this code:
JObject o = JObject.Parse(jsStr);
IEnumerable<JToken> selEnum = o.SelectTokens(theFilter);
where the jsStr
is the content of https://api.github.com/search/repositories?q=Newtonsoft.Json&sort=stars&order=desc, and theFilter
can be any valid JPATH query string (e.g., ".items"
or ".items[*].owner"
).
How to return the selected as a valid json string?
回答1:
It sounds like you just need Json.SerializeObject
:
var o = JObject.Parse(jsStr);
var selEnum = o.SelectTokens(theFilter);
var newJson = JsonConvert.SerializeObject(selEnum);
This will give you JSON representing an array of all of the owner
values from the original JSON.
来源:https://stackoverflow.com/questions/42304105/newtonsoft-json-how-to-code-generic-selection