Question

I got a json string from http://itunes.apple.com/lookup?id=796171602

this link always give me an array with only one Item. so instead of creating a class with root object and results array, I want to get the only first object and work with directly.

        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);

when deserializing it, I get:

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.

when I create a c# class using jsonpack.com it works.

my custom object is this:

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>();
    }
}

last night it works well, but today isn't working. doesn anyone have any idea?

Était-ce utile?

La solution

this code works for your json string

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; }
}

EDIT

var obj =  JsonConvert.DeserializeObject<JObject>(appJsonData)["results"][0] as JObject;
var app = obj.ToObject <Result>();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top