【问题标题】:Get id from a Spotify JSON response with NewtonsoftJSON使用 NewtonsoftJSON 从 Spotify JSON 响应中获取 id
【发布时间】:2021-09-13 16:48:22
【问题描述】:

我需要使用 C# 中的 NewtonsoftJSON 从这个 JSON 响应中获取“id”数据:

{
  "devices": [
    {
      "id": "0fa1a5aa7fdf4438a8082c993be1d9cd9bf0d32b",
      "is_active": false,
      "is_private_session": false,
      "is_restricted": false,
      "name": "DESKTOP-89E1DLU",
      "type": "Computer",
      "volume_percent": 26
    },
    {
      "id": "404bdc9492d312f104359d72c3ee55dd7f624120",
      "is_active": false,
      "is_private_session": false,
      "is_restricted": false,
      "name": "Fluent Spotify",
      "type": "Computer",
      "volume_percent": 100
    }
  ]
}

我需要始终从包含 “Fluent Spotify” 名称的数据块中获取 “id” 数据。

为此的 Spotify 文档游乐场是:https://developer.spotify.com/console/get-users-available-devices/

那么有人可以告诉我如何解析这个吗?我是 C# 新手。谢谢

【问题讨论】:

  • 请拨打tour。你都尝试了些什么?请发布您的代码和您遇到的任何错误。 Newtonsoft 网站提供了关于如何在 C# 中反序列化 JSON 对象的很好的示例link
  • 如果答案已经解决了您的问题,请mark 它被稍后访问此主题的方便人士所接受

标签: c# xaml uwp json.net spotify


【解决方案1】:

services 可以从 json sn-p 为您生成 c# 类。这些在您的 JSON 示例中做得很好:

public class Device
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("is_active")]
    public bool IsActive { get; set; }

    [JsonProperty("is_private_session")]
    public bool IsPrivateSession { get; set; }

    [JsonProperty("is_restricted")]
    public bool IsRestricted { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }

    [JsonProperty("volume_percent")]
    public int VolumePercent { get; set; }
}

public class Root
{
    [JsonProperty("devices")]
    public List<Device> Devices { get; set; }
}

一个你有那些你将json解析为Root对象的类

Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 

然后你就可以使用 LINQ

var id = myDeserializedClass.Devices.First(x => x.Name == "Fluent Spotify").Id;

注意:以上假设总是有一个名称为“Fluent Spotify”的项目 - 如果没有,则会发生错误。如果您需要处理这种情况,请酌情使用FirstOrDefaultSingleOrDefault

【讨论】:

    【解决方案2】:

    您可以在 newtonsoft 中阅读 Querying JSON with LINQSelect Token

    试试这个:

    string json = @"{
      "devices": [
        {
          "id": "0fa1a5aa7fdf4438a8082c993be1d9cd9bf0d32b",
          "is_active": false,
          "is_private_session": false,
          "is_restricted": false,
          "name": "DESKTOP-89E1DLU",
          "type": "Computer",
          "volume_percent": 26
        },
        {
          "id": "404bdc9492d312f104359d72c3ee55dd7f624120",
          "is_active": false,
          "is_private_session": false,
          "is_restricted": false,
          "name": "Fluent Spotify",
          "type": "Computer",
          "volume_percent": 100
        }
      ]
    }";
    
    JObject spotify = JObject.Parse(json);
    string id = (string)spotify["devices"][1]["id"];
    // id = 404bdc9492d312f104359d72c3ee55dd7f624120
    

    或尝试条件:

    JObject o = JObject.Parse(@"{
      "devices": [
        {
          "id": "0fa1a5aa7fdf4438a8082c993be1d9cd9bf0d32b",
          "is_active": false,
          "is_private_session": false,
          "is_restricted": false,
          "name": "DESKTOP-89E1DLU",
          "type": "Computer",
          "volume_percent": 26
        },
        {
          "id": "404bdc9492d312f104359d72c3ee55dd7f624120",
          "is_active": false,
          "is_private_session": false,
          "is_restricted": false,
          "name": "Fluent Spotify",
          "type": "Computer",
          "volume_percent": 100
        }
      ]
    }");
    
    JToken spotify = o.SelectToken("$.devices[?(@.name == 'Fluent Spotify')]");
    var id = spotify.Value<string>("id");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 2022-06-28
      • 2014-09-16
      • 1970-01-01
      相关资源
      最近更新 更多