0

Below code is used for deserializing json string from URL

string s = "http://stgxx.xxapixx.xyxxxz.com/data/statistics.json?apiversion=5.4&passkey=xyxxx&filter=productid:test1&stats=NativeReviews";

using (WebClient wc = new WebClient())  
{
   string s1 = wc.DownloadString(s);

   byte[] byteArray = Encoding.UTF8.GetBytes(s1);
   MemoryStream stream = new MemoryStream(byteArray);

   DataContractJsonSerializer serializer =
        new DataContractJsonSerializer(typeof(BVJSONRatings));
   stream.Position = 0;

   BVJSONRatings yourObject = (BVJSONRatings)serializer.ReadObject(stream);
}

This is the json response format

{
"Errors": [], 
"HasErrors": false, 
"Includes": {}, 
"Limit": 10, 
"Locale": "en_US", 
"Offset": 0, 
"Results": [
    {
        "ProductStatistics": {
            "NativeReviewStatistics": {
                "AverageOverallRating": 5, 
                "OverallRatingRange": 5, 
                "TotalReviewCount": 1
            }, 
            "ProductId": "test3", 
            "ReviewStatistics": {
                "AverageOverallRating": 3.8333, 
                "OverallRatingRange": 5, 
                "TotalReviewCount": 6
             },
     }        
], 
"TotalResults": 1
}

I'm using the below objects to map the above json to them

 public class BVJSONRatings
{
    public string ProductId;
    public string AverageOverallRating;
    public string TotalReviewCount;
    public string TotalResults;
    public IList<Results> Results { get; set; }
}

public class Results
{
    public IList<ProductStatistics> ProductStatistics { get; set; }
    public IList<string> ReviewStatistics { get; set; }
}

public class ProductStatistics
{
    public string TotalReviewCount;
    public string AverageOverallRating;
}

In the process of deserializing I'm not getting the values within "Results" all I get is just "TotalResults": 1.

3
  • The JSON you posted is invalid and your classes are not even close to what they should be to map the JSON on. Also it's not a good idea to share keys in your post, like the passkey Commented Dec 8, 2016 at 8:30
  • @Jim passkey values are not real ones. Commented Jul 15, 2019 at 6:11
  • That is a good thing :) Commented Jul 16, 2019 at 11:21

1 Answer 1

2

That's the only object that corresponds with your JSON. You have to use [JsonProperty("propertyName")] for each attribute.

And since your object contains a list of results, your Results should look like this: IList<Results> Results { get; set; }

Your class could look like this:

public RootObject()
{
    [JsonProperty("HasErrors")] //This will point to your JSON attribute 'HasErrors'
    public bool Errors { get; set; } //Note that the name is different, but it will still deserialize.

    [JsonProperty("Limit")]
    public int Limit { get; set; }

    public IList<Results> Results { get; set; }
    //Etc...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.