使用 NewtonsoftJSON 从 Spotify JSON 响应中获取 ID

Get id from a Spotify JSON response with NewtonsoftJSON

我需要使用 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# 的新手。谢谢

您可以在 newtonsoft

中阅读 Querying JSON with LINQ and Select 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");

services 可以从 json 片段为您生成 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