我仍然是C#和API的新手。直到昨天我才意识到我没有得到我在回复中期待的所有数据时,我对这个API没有任何问题。
我要做的是调用此API链接以获取JSON响应,然后将其转换为XML并保存文档,这完美但该端点有页面大小限制。
回复中有 odata.nextLink ,但我不知道如何使用它。
我所尝试的是"搜索"文本字符串" odata.nextLink"的响应。我能够搜索该字符串并且它给了我所需的值,但是我无法弄清楚如何在do-while循环中使用它来附加每个响应的数据。
非常感谢任何帮助我指向正确方向的帮助。
以下是我正在使用的代码(出于安全原因阻止了Access_Token)
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using RestSharp;
namespace GetProducts
{
public class WebRequestGet
{
public static void Main()
{
var client = new RestClient("https://api.channeladvisor.com/v1/Products?access_token=*******************&$select=Sku,ID&$count=true");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
var content = response.Content;
string json = content;
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json, "Channeladvisor");
doc.Save(@"C:\Users\NHead\Documents\Visual Studio 2015\Projects\GetProducts\GetProducts\bin\Debug\Response.xml");
Console.WriteLine(content);
Console.ReadLine();
}
}
}
这是我在转换为XML之前得到的响应。
{
"@odata.context":"https://api.channeladvisor.com/v1/$metadata#Products(Sku,ID)","@odata.count":10728,"value":[
{
"Sku":"650SheetSets-P","ID":4932487
},{
"Sku":"400SheetSets-P","ID":4932488
},{
"Sku":"1500SheetSets-P","ID":4932489
},{
"Sku":"650TWSH SL WH","ID":4932510
},{
"Sku":"650TWSH SL BL","ID":4932511
},{
"Sku":"650TWSH SL GD","ID":4932512
},{
"Sku":"650TWSH SL MDBL","ID":4932513
},{
"Sku":"650TWSH SL IV","ID":4932514
},{
"Sku":"650TWSH SL BGY","ID":4932515
},{
"Sku":"650TWSH SL SKBL","ID":4932516
},{
"Sku":"650TWSH SL CHC","ID":4932517
},{
"Sku":"650TWSH SL SG","ID":4932518
},{
"Sku":"650TWSH SL TP","ID":4932519
},{
"Sku":"650SKSH SL WH","ID":4932520
},{
"Sku":"650SKSH SL BL","ID":4932521
},{
"Sku":"650SKSH SL GLD","ID":4932522
},{
"Sku":"650SKSH SL MDBL","ID":4932523
},{
"Sku":"650SKSH SL IV","ID":4932524
},{
"Sku":"650SKSH SL BGY","ID":4932525
},{
"Sku":"650SKSH SL SKBL","ID":4932526
},{
"Sku":"650SKSH SL CHC","ID":4932527
},{
"Sku":"650SKSH SL SG","ID":4932528
},{
"Sku":"650SKSH SL TP","ID":4932529
},{
"Sku":"650QNSH SL WH","ID":4932530
},{
"Sku":"650QNSH SL BL","ID":4932531
},{
"Sku":"650QNSH SL GLD","ID":4932532
},{
"Sku":"650QNSH SL MDBL","ID":4932533
},{
"Sku":"650QNSH SL IV","ID":4932534
},{
"Sku":"650QNSH SL BGY","ID":4932535
},{
"Sku":"650QNSH SL SKBL","ID":4932536
},{
"Sku":"650QNSH SL CHC","ID":4932537
},{
"Sku":"650QNSH SL SG","ID":4932538
},{
"Sku":"650QNSH SL TP","ID":4932539
},{
"Sku":"650KGSH SL WH","ID":4932540
},{
"Sku":"650KGSH SL BL","ID":4932541
},{
"Sku":"650KGSH SL GLD","ID":4932542
},{
"Sku":"650KGSH SL MDBL","ID":4932543
},{
"Sku":"650KGSH SL IV","ID":4932544
},{
"Sku":"650KGSH SL BGY","ID":4932545
},{
"Sku":"650KGSH SL SKBL","ID":4932546
},{
"Sku":"650KGSH SL CHC","ID":4932547
},{
"Sku":"650KGSH SL SG","ID":4932548
},{
"Sku":"650KGSH SL TP","ID":4932549
},{
"Sku":"650FLSH SL WH","ID":4932550
},{
"Sku":"650FLSH SL BL","ID":4932551
},{
"Sku":"650FLSH SL GLD","ID":4932552
},{
"Sku":"650FLSH SL MDBL","ID":4932553
},{
"Sku":"650FLSH SL IV","ID":4932554
},{
"Sku":"650FLSH SL BGY","ID":4932555
},{
"Sku":"650FLSH SL SKBL","ID":4932556
},{
"Sku":"650FLSH SL CHC","ID":4932557
},{
"Sku":"650FLSH SL SG","ID":4932558
},{
"Sku":"650FLSH SL TP","ID":4932559
},{
"Sku":"650CKSH SL WH","ID":4932560
},{
"Sku":"650CKSH SL BL","ID":4932561
},{
"Sku":"650CKSH SL GLD","ID":4932562
},{
"Sku":"650CKSH SL MDBL","ID":4932563
},{
"Sku":"650CKSH SL IV","ID":4932564
},{
"Sku":"650CKSH SL BGY","ID":4932565
},{
"Sku":"650CKSH SL SKBL","ID":4932566
},{
"Sku":"650CKSH SL CHC","ID":4932567
},{
"Sku":"650CKSH SL SG","ID":4932568
},{
"Sku":"650CKSH SL TP","ID":4932569
},{
"Sku":"400TWSH SL WH","ID":4932598
},{
"Sku":"400TWSH SL IV","ID":4932602
},{
"Sku":"400TWSH SL BGY","ID":4932603
},{
"Sku":"400TWSH SL SKBL","ID":4932604
},{
"Sku":"400TWSH SL SG","ID":4932605
},{
"Sku":"400TWSH SL CHC","ID":4932606
},{
"Sku":"400TWSH SL TP","ID":4932607
},{
"Sku":"400QNSH SL WH","ID":4932608
},{
"Sku":"400QNSH SL IV","ID":4932612
},{
"Sku":"400QNSH SL BGY","ID":4932613
},{
"Sku":"400QNSH SL SKBL","ID":4932614
},{
"Sku":"400QNSH SL CHC","ID":4932615
},{
"Sku":"400QNSH SL SG","ID":4932616
},{
"Sku":"400QNSH SL TP","ID":4932617
},{
"Sku":"400SKSH SL WH","ID":4932618
},{
"Sku":"400SKSH SL IV","ID":4932622
},{
"Sku":"400SKSH SL BGY","ID":4932623
},{
"Sku":"400SKSH SL SKBL","ID":4932624
},{
"Sku":"400SKSH SL CHC","ID":4932625
},{
"Sku":"400SKSH SL SG","ID":4932626
},{
"Sku":"400SKSH SL TP","ID":4932627
},{
"Sku":"400KGSH SL WH","ID":4932628
},{
"Sku":"400KGSH SL IV","ID":4932632
},{
"Sku":"400KGSH SL BGY","ID":4932633
},{
"Sku":"400KGSH SL SKBL","ID":4932634
},{
"Sku":"400KGSH SL CHC","ID":4932635
},{
"Sku":"400KGSH SL SG","ID":4932636
},{
"Sku":"400KGSH SL TP","ID":4932637
},{
"Sku":"400FLSH SL WH","ID":4932638
},{
"Sku":"400FLSH SL GLD","ID":4932640
},{
"Sku":"400FLSH SL IV","ID":4932642
},{
"Sku":"400FLSH SL BGY","ID":4932643
},{
"Sku":"400FLSH SL SKBL","ID":4932644
},{
"Sku":"400FLSH SL CHC","ID":4932645
},{
"Sku":"400FLSH SL SG","ID":4932646
},{
"Sku":"400FLSH SL TP","ID":4932647
},{
"Sku":"400CKSH SL WH","ID":4932648
}
],"@odata.nextLink":"https://api.channeladvisor.com/v1/Products?access_token=***************************************&$select=Sku%2CID&$count=true&$skip=100"
}
答案 0 :(得分:0)
我认为这可能会对你有所帮助
public static void Main()
{
do
{
var client = new RestClient("https://api.channeladvisor.com/v1/Products?access_token=*******************&$select=Sku,ID&$count=true");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
var content = response.Content;
string json = content;
}
while(content.Contains("odata.nextLink"))
//rest of your code
}
但是这里的情况是你需要读取nextLink的值。因此你可以使用Newtonsoft来阅读它。但我在这里指出的一点是,在nextLink中,它只是在URL的末尾添加了$skip=100
。所以我假设如果你有更多的数据,它下次会添加$skip=200
因此你可以有一个解决方法而不创建类来读取完整的JSON
public static void Main()
{
string myUrl = "https://api.channeladvisor.com/v1/Products?access_token=*******************&$select=Sku,ID&$count=true";
int cntskip = 0;
do
{
if(cntskip>0)
{
myUrl += "$skip=" + cntskip;
}
var client = new RestClient(myUrl);
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
var content = response.Content;
string json = content;
cntskip += 100;
}
while(content.Contains("odata.nextLink"))
//rest of your code
}