反序列化json对象时,输入不是有效的Base-64

时间:2014-02-19 20:31:13

标签: c# json base64 json.net json-deserialization

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

获得了一个json字符串

这个链接总是给我一个只有一个项目的数组。所以我没有创建一个具有根对象和结果数组的类,而是希望获得唯一的第一个对象并直接使用。

        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创建一个c#类时,它可以工作。

我的自定义对象是:

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>();
    }
}
昨晚它运作良好,但今天不行。 没有人有任何想法吗?

1 个答案:

答案 0 :(得分: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>();