简体   繁体   English

C# 无法反序列化当前的 JSON 对象 (Newtonsoft)

[英]C# Cannot deserialize the current JSON object (Newtonsoft)

How do I get this array as string?如何将此数组作为字符串获取? unfortunately (I am a beginner) i only know how to get only arrays or only objects but not how to "mix"不幸的是(我是初学者)我只知道如何只获取数组或只获取对象,但不知道如何“混合”

在此处输入图片说明

    public class TrackContent
    {
        [JsonProperty("track")] public Album Album { get; set; }
    }

    public class Album
    {
        [JsonProperty("album")] public CoverImage CoverImage { get; set; }
    }

    public class CoverImage
    {
        [JsonProperty("image")] public Number Number { get; set; }
    }

    public class Number
    {
        [JsonProperty("3")] public ImageUrl ImageUrl { get; set; }
    }

    public class ImageUrl
    {
        [JsonProperty("#text")] public string Name { get; set; }
    }

    private static void Main(string[] args)
    {
        const string auth = "http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=XXXXXXXXXXXXXXXXXXX&artist=Ariana%20Grande&track=One%20Last%20Time&format=json";

        var httpClient = new HttpClient();
        var jsonInfo = httpClient.GetStringAsync(auth);
        var deserializeJson = JsonConvert.DeserializeObject <TrackContent>(jsonInfo.Result);
        Console.WriteLine(deserializeJson.Album.CoverImage.Number.ImageUrl.Name);
    }
public class Streamable
{
    [JsonProperty("#text")] public string text { get; set; }
    public string fulltrack { get; set; }
}

public class Artist
{
    public string name { get; set; }
    public string mbid { get; set; }
    public string url { get; set; }
}

public class Image
{
    [JsonProperty("#text")] public string text { get; set; }
    public string size { get; set; }
}

public class Attr
{
    public string position { get; set; }
}

public class Album
{
    public string artist { get; set; }
    public string title { get; set; }
    public string mbid { get; set; }
    public string url { get; set; }
    public List<Image> image { get; set; }
    [JsonProperty("@attr")] public Attr attr { get; set; }
}

public class Tag
{
    public string name { get; set; }
    public string url { get; set; }
}

public class Toptags
{
    public List<Tag> tag { get; set; }
}

public class Track
{
    public string name { get; set; }
    public string mbid { get; set; }
    public string url { get; set; }
    public string duration { get; set; }
    public Streamable streamable { get; set; }
    public string listeners { get; set; }
    public string playcount { get; set; }
    public Artist artist { get; set; }
    public Album album { get; set; }
    public Toptags toptags { get; set; }
}

public class RootObject
{
    public Track track { get; set; }
}

Then:然后:

var deserializeJson = JsonConvert.DeserializeObject<RootObject>(jsonInfo.Result);
Console.WriteLine(deserializeJson.Album.image[3].text);

Edit: That way, you can deserialize any JSON response from the API into the C# RootObject and you can access any of the properties / values you wish.编辑:这样,您可以将来自 API 的任何 JSON 响应反序列化到 C# RootObject 中,并且您可以访问您希望的任何属性/值。

I suggest using a tool such as this http://json2csharp.com/ to generate your C# Classes from a JSON string.我建议使用诸如http://json2csharp.com/ 之类的工具从 JSON 字符串生成 C# 类。

This will then give you the classes you need, and you can then deserialise the "RootObject" using Newtonsoft.这将为您提供所需的类,然后您可以使用 Newtonsoft 反序列化“RootObject”。

Edit:编辑:
And if you want to go the other way, then this tool can be helpful https://csharp2json.io/如果你想走另一条路,那么这个工具会很有帮助https://csharp2json.io/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM