簡體   English   中英

使用 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# 新手。 謝謝

有一些服務可以從 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

您可以在 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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM