在 C# 中使用 datacontract 序列化程序反序列化 json 字符串
Deserialize json string using datacontract serializer in c#
下面的代码用于从 URL
反序列化 json 字符串
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);
}
这是json响应格式
{
"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
}
我正在使用以下对象将上面的 json 映射到它们
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;
}
在反序列化过程中,我没有得到 "Results" 内的值,我得到的只是 "TotalResults": 1.
这是唯一与您的 JSON 相对应的对象。您必须为每个属性使用 [JsonProperty("propertyName")]
。
并且由于您的对象包含结果列表,因此您的 Results
应该如下所示:IList<Results> Results { get; set; }
您的 class 可能如下所示:
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...
}
下面的代码用于从 URL
反序列化 json 字符串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);
}
这是json响应格式
{
"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
}
我正在使用以下对象将上面的 json 映射到它们
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;
}
在反序列化过程中,我没有得到 "Results" 内的值,我得到的只是 "TotalResults": 1.
这是唯一与您的 JSON 相对应的对象。您必须为每个属性使用 [JsonProperty("propertyName")]
。
并且由于您的对象包含结果列表,因此您的 Results
应该如下所示:IList<Results> Results { get; set; }
您的 class 可能如下所示:
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...
}