我可以 LINQ 一个 JSON 吗?

问题描述:

这是我从 .NET 上的请求中得到的 JSON:

This is the JSON I get from a request on .NET:

{
  "id": "110355660738", 
  "picture": {
    "data": {
      "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/1027085_12033235063_5234302342947_n.jpg", 
      "is_silhouette": false
    }
  }
}

我想使用(也许?)LINQ来捕捉url"字段.我做了很多这样的请求,这有点不同.所以我不会每次都创建一个 C# 类并反序列化它.

and I'd like to catch the field "url", using (maybe?) LINQ. I do many request as this, that differents a bit. So I won't to create a C# Class and deserialize it every time.

是一种提取单个字段的方法吗?谢谢!

Is it a way to extract a single field? Thank you!

不需要 Linq,只需使用 dynamic(使用 Json.Net)

No need for Linq, just use dynamic (using Json.Net)

dynamic obj = JObject.Parse(json);
Console.WriteLine((string)obj.picture.data.url);

Linq 版本可读性不高

Linq version would not be much readable

JObject jObj = JObject.Parse(json);
var url = (string)jObj.Descendants()
                    .OfType<JProperty>()
                    .Where(p => p.Name == "url")
                    .First()
                    .Value;

文档:LINQ to JSON

Documentation: LINQ to JSON