Reputation: 252
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.
Upvotes: 0
Views: 2621
Reputation:
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...
}
Upvotes: 2