使用多个属性反序列化JSON

时间:2014-09-23 16:06:55

标签: c# json .net-4.0 deserialization

这次我正在尝试将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
            }

1 个答案:

答案 0 :(得分:0)

好的,我找到了一个有效但不太完美的解决方案。

首先,我需要获取允许我知道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);
    }

使用此列表我创建的列表中包含我想要的链接,这些链接位于属性“zoom”内,将填充每个pdf的所有页面链接