2014-09-23 76 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

相关问题