正在反序列化具有多个属性的JSON
本文关键字:属性 JSON 反序列化 | 更新日期: 2023-09-27 18:24:09
这一次我试图反序列化对请求的json响应。完整的json可以在这里找到:http://pastebin.com/V0hAxFmj
public class Folheto
{
[JsonProperty("id")]
public string id { get; set; }
[JsonProperty("zoom")]
public string imagem { get; set; }
[JsonProperty("pageCount")]
public int pageCount { get; set; }
[JsonProperty("title")]
public string nome { get; set; }
}
最好的解决方案是有一个字符串列表,其中包含路径["pages][pagenumber][imageUrls]中的所有"缩放"链接。
我是不是错过了什么?
编辑:JSON代码
{
"directBrochureUrl": "http://www.ofertia.com/catalogo-305846837",
"id": 305846837,
"pageCount": 8,
"pages": {
"1": {
"imageUrls": {
"normal": "http://static01.ofertia.com/catalogos/84e0a539-f687-4682-b6f8-b29e79f8de87/0/normal.v1.jpg",
"zoom": "http://static01.ofertia.com/catalogos/84e0a539-f687-4682-b6f8-b29e79f8de87/0/large.v1.jpg",
"zoomSize": "{1079,1600}"
},
"productOverlays": [
]
},
},
"poll": {
"hasPoll": false,
"hasPollImage": null,
"mobileLongImage": null,
"mobileSquareImage": null,
"pollUrl": null,
"webImage": null
},
"retailerId": 84243242,
"retailerName": "Dia Market",
"sector": {
"iconUrl": "http://static01.ofertia.com/theme/logo-100497.v71.png",
"id": 100497,
},
"showAdsInVisualizer": false,
"title": "Calidad y precio están muy cerca",
"validFrom": "2014-09-11T00:00:00",
"validUntil": "2014-09-24T23:00:00"
}
EDIT2(请求代码):
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.ofertia.com/api/v1/brochure/static/305846837");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0";
request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.5");
request.Headers.Add("X-Requested-With", @"XMLHttpRequest");
request.Referer = "http://www.ofertia.com/catalogo";
request.KeepAlive = true;
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "application/json";
try
{
using (WebResponse response = request.GetResponse())
{
var responseValue = string.Empty;
// grab the response
using (var responseStream = response.GetResponseStream())
{
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
if (responseValue != "")
{
Folheto jsonModel = JsonConvert.DeserializeObject<Folheto>(responseValue);
string _id = jsonModel.id;
string _nome = jsonModel.nome;
string _link_imagem = jsonModel.imagem;
int num_pag =jsonModel.pageCount;
}
}
}
catch (WebException ex)
{
// Handle error
}
好的,我找到了一个有效但不太完美的解决方案。
首先,我需要采取的属性,让我知道pdf的总页数:
FolhetoOfertia jsonModel = JsonConvert.DeserializeObject<FolhetoOfertia>(responseValue);
int num_pag = jsonModel.pageCount;
(num_pag是让我在下一步循环页面的变量)
第二,我解析我的请求中的答案(答案在变量responseValue中),并使用它正在搜索的页面编号进行循环,无论页面数量是多少,这都会起作用,因为我得到了实际值,不需要使用假的高数字
var jObj = JObject.Parse(responseValue);
for (int pag = 1; pag < num_pag + 1; pag++)
{
string valores = jObj["pages"][pag.ToString()]["imageUrls"]["zoom"].ToString();
lista_links.Add(valores);
}
有了这个,我创建的列表中有我想获取的链接,这些链接位于属性"缩放"内,将填充每个pdf 的所有页面链接