2 回答

TA貢獻1836條經驗 獲得超5個贊
是的,有一個用于動態查詢的 API。
代碼看起來像這樣:
JObject rss = JObject.Parse(json);?
var postTitles =
? ? from p in rss["channel"]["item"]
? ? select (string)p["title"];

TA貢獻1765條經驗 獲得超5個贊
終于搞清楚這個api了
一些 JToken 條目具有值列表,其他條目具有名稱和值。在解析之前,您必須先對哪個是哪個進行排序。
這將創建一個包含 Json 文件中每個條目的字典
void SomeFunction()
{
Dictionary<string, decimal> json_data = new Dictionary<string, decimal>();
dynamic json_obj = JsonConvert.DeserializeObject(json);
Linearize(ref json_data, json_obj);
}
void Linearize(ref Dictionary<string, decimal> input_dict, JToken json_data, string key = "")
{
int i;
if (json_data != null)
{
if (json_data.HasValues)
{
i = 0;
foreach (dynamic entry in json_data)
{
//Add a Name field if it exists
Type typeOfDynamic = entry.GetType();
if (typeOfDynamic.GetProperties().Where(p => p.Name.Equals("Name")).Any())
key += entry.Name + ".";
//If JToken is an Array
if (((JToken)entry).HasValues)
{
Linearize(ref input_dict, entry, key + "[" + i++ + "]" + ".");
}
//If JToken is a data type
else if (entry.Type == JTokenType.String || entry.Type == JTokenType.Float || entry.Type == JTokenType.Integer)
{
decimal output;
if (decimal.TryParse(entry.ToString(), out output))
input_dict.Add(key + "[" + i++ + "]", output);
}
}
}
}
}
- 2 回答
- 0 關注
- 296 瀏覽
添加回答
舉報