0

我从http://itunes.apple.com/lookup?id=796171602

这个链接总是给我一个只有一个项目的数组。因此,我不想使用根对象和结果数组创建一个类,而是要获取唯一的第一个对象并直接使用。

        WebClient webClient = new WebClient();
        webClient.Encoding = Encoding.UTF8;
        string appJsonData = webClient.DownloadString("http://itunes.apple.com/lookup?id=796171602");

        App app = new App();
        JToken rootObject = JToken.Parse(appJsonData);
        JToken appToken = rootObject["results"];

        app = JsonConvert.DeserializeObject<App>(appJsonData);

反序列化时,我得到:

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

当我使用 jsonpack.com 创建 ac# 类时,它可以工作。

我的自定义对象是这样的:

public class App : Entity
{
    //it's not app id in the data base
    [JsonProperty("trackId")]
    public virtual int AppStoreId { get; set; }

    [JsonProperty("trackViewUrl")]
    public virtual string AppStoreLink { get; set; }

    [JsonProperty("sellerName")]
    public virtual string SellerName { get; set; }

    [JsonProperty("artworkUrl60")]
    public virtual string Icon { get; set; }

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

    [JsonProperty("formattedPrice")]
    public virtual string FormattedPrice { get; set; }

    [JsonProperty("primaryGenreName")]
    public virtual string Genre { get; set; }

    [JsonProperty("Description")]
    public virtual string Description { get; set; }

    [JsonProperty("releaseNotes")]
    public virtual string ReleaseNotes { get; set; }

    [JsonProperty("releaseDate")]
    public virtual DateTime ReleaseDate { get; set; }

    [JsonProperty("userRatingCountForCurrentVersion")]
    public virtual string UserRatingCountForCurrentVersion { get; set; }

    [JsonProperty("averageUserRatingForCurrentVersion")]
    public virtual string AverageUserRatingForCurrentVersion { get; set; }

    [JsonProperty("fileSizeBytes")]
    public virtual string FileSize { get; set; }

    [JsonProperty("screenshotUrls")]
    public virtual IList<string> IPhoneSceenShots { get; set; }

    [JsonProperty("ipadscreenshotUrls")]
    public virtual IList<string> IPadSceenShots { get; set; }

    public App()
    {
        IPhoneSceenShots = new List<string>();
        IPadSceenShots = new List<string>();
    }
}

昨晚还好好的,今天就不行了。有人知道吗?

4

1 回答 1

1

此代码适用于您的 json 字符串

var obj = JsonConvert.DeserializeObject<RootObject>(appJsonData);

public class Result
{
    public string kind { get; set; }
    public List<object> features { get; set; }
    public List<string> supportedDevices { get; set; }
    public bool isGameCenterEnabled { get; set; }
    public List<string> screenshotUrls { get; set; }
    public List<object> ipadScreenshotUrls { get; set; }
    public string artworkUrl60 { get; set; }
    public string artworkUrl512 { get; set; }
    public string artistViewUrl { get; set; }
    public int artistId { get; set; }
    public string artistName { get; set; }
    public double price { get; set; }
    public string version { get; set; }
    public string description { get; set; }
    public string currency { get; set; }
    public List<string> genres { get; set; }
    public List<string> genreIds { get; set; }
    public string releaseDate { get; set; }
    public string sellerName { get; set; }
    public string bundleId { get; set; }
    public int trackId { get; set; }
    public string trackName { get; set; }
    public string primaryGenreName { get; set; }
    public int primaryGenreId { get; set; }
    public string releaseNotes { get; set; }
    public string formattedPrice { get; set; }
    public string wrapperType { get; set; }
    public string trackCensoredName { get; set; }
    public List<string> languageCodesISO2A { get; set; }
    public string fileSizeBytes { get; set; }
    public string contentAdvisoryRating { get; set; }
    public double averageUserRatingForCurrentVersion { get; set; }
    public int userRatingCountForCurrentVersion { get; set; }
    public string artworkUrl100 { get; set; }
    public string trackViewUrl { get; set; }
    public string trackContentRating { get; set; }
}

public class RootObject
{
    public int resultCount { get; set; }
    public List<Result> results { get; set; }
}

编辑

var obj =  JsonConvert.DeserializeObject<JObject>(appJsonData)["results"][0] as JObject;
var app = obj.ToObject <Result>();
于 2014-02-19T20:34:21.457 回答