Can I LINQ a JSON?

前端 未结 3 446
谎友^
谎友^ 2020-11-29 04:28

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

{
  \"id\": \"110355660738\", 
  \"picture\": {
    \"data\": {
      \"url\": \"https://fbcdn-profile-a.akama         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 04:55

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

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

    Linq version would not be much readable

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

    Documentation: LINQ to JSON

提交回复
热议问题