2014-09-23 73 views
0

這次我試圖反序列化一個json響應我的請求。 完整的JSON可以在這裏找到:http://pastebin.com/V0hAxFmj反序列化具有多個屬性的JSON

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; } 
} 

最好的解決辦法是有一個包含路徑[「頁] [頁面編號] [imageUrls]所有「縮放」鏈接字符串列表

我缺少的東西

編輯:?JSON CODE

{ 
    "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

這就是你的樣品中大量JSON的所有網頁的鏈接進行填充。如果你用一小段JSON提供一個* short *,但完整的程序來演示這個問題,那真的很有幫助。 (這並不是說它不是原來的JSON,而是一些奇怪的格式化版本。) – 2014-09-23 16:13:11

+0

對不起沒有找到如何正確格式化。 pastebin代碼是我正在嘗試閱讀的真正完整答案,我只是使用郵遞員來獲得更漂亮,更易於閱讀的答案版本。 如果你知道我在哪裏可以粘貼它,所以它很容易和很好的閱讀我將在這裏發佈在此期間,我可以採取截圖 http://prntscr.com/4pl3ih part1 http://prntscr.com/ 4pl3s2 part2 – Jorge 2014-09-23 16:21:57

+0

你不應該把它放在pastebin中。你應該有一個* short *的例子,可以直接包含在問題中。您只需要約20行代碼和10行JSON(如果有的話) - 我們應該能夠將json複製到一個文件中(或者只是將其包含在代碼中),將代碼複製並粘貼到另一個文件中,編譯並運行。屏幕截圖確實無助於我們重現問題。 – 2014-09-23 16:25:42

回答

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); 
    } 

這個我創建有我想拿屬於財產「變焦」中的鏈接列表將與每個PDF

相關問題