1 回答
TA贡献1824条经验 获得超8个赞
根据您对“最佳”的定义:
在 JSON 中某处查找属性的最短方法是将 JSON 解析为 a JObject,然后SelectToken与递归下降JsonPath 表达式一起使用:
public static string FindFirst(string json, string propertyName){
return JObject.Parse(json).SelectToken("$.." + propertyName)?.ToString();}小提琴:https ://dotnetfiddle.net/JQxu9c
我知道用 Json.Net 做同样事情的最快JsonTextReader方法是使用:
public static string FindFirst(string json, string propertyName)
{
using (StringReader sr = new StringReader(json))
using (JsonReader reader = new JsonTextReader(sr))
{
while (reader.Read())
{
if (reader.TokenType == JsonToken.PropertyName &&
reader.Value.ToString() == propertyName)
{
return reader.ReadAsString();
}
}
return null;
}
}
小提琴:https ://dotnetfiddle.net/aR3qVe
- 1 回答
- 0 关注
- 130 浏览
添加回答
举报
