Newtonsoft.Json, How to code generic selection

旧城冷巷雨未停 提交于 2019-12-12 03:45:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!