REST JSON,服务器忽略50条记录,获取所有记录(以当前示例更新)

时间:2018-09-11 13:10:19

标签: c# json restsharp

我正在使用REST API来获取JSON。所有者施加了50个结果的限制。有人可以建议任何有关如何获取所有记录然后将其反序列化为列表的链接/教程吗?

我正在使用RestSharp和C#

自从昨天发布这个问题以来,我一直在尝试解决一个创建新方法的解决方案,该方法可用于所有REST调用。我有这个:

    public T getAllRecords<T>(List<T> genericList, string RestCommand, string[,] parameters)
    {

        int pageCount = 50;
        var client = new RestClient("https://******************************");
        client.Authenticator = new HttpBasicAuthenticator("*******", "*******");

        var request = new RestRequest(RestCommand, Method.GET);
        // add the parameters
        for (int i = 0; i < parameters.Length / 2; i++)
        {
            request.AddParameter(parameters[i, 0], parameters[i, 1]);
        }


            var response = client.Execute(request);
        var content = response.Content;

        if (typeof(T) == typeof(Rootobject))
        {
            List<Rootobject> tempObject = new List<Rootobject>();
            tempObject.Cast<Rootobject>();
            Rootobject myRel = JsonConvert.DeserializeObject<Rootobject>(content);
            tempObject.Add(myRel);

            // create logic for looping through remaining records
            int totalRecords = myRel.meta.pageInfo.totalResults;
            do
            {
                request.AddParameter("startAt", pageCount);
                response = client.Execute(request);
                content = response.Content;
                Rootobject myRel1 = JsonConvert.DeserializeObject<Rootobject>(content);
                tempObject.Add(myRel1);
                pageCount += 50;
            } while (pageCount <= totalRecords);

            List<Item.Datum> totalList = new List<Item.Datum>();
            foreach (Rootobject rootItem in tempObject)
            {
                foreach (Item.Datum item in rootItem.data)
                {
                    totalList.Add(item);
                }
            }

            return (T)Convert.ChangeType(totalList, typeof(Item.Datum));


        }

        return default(T);

    }

我这样称呼它:

var allItems = getAllRecords(new List<Rootobject>(), "abstractitems", new string[,] { { "project", "001" }, { "itemType", "151" }, { "maxResults", "50" } });

我期望它重新调整 Item.Datum 类型的对象的列表,但是它出现以下错误:

System.InvalidCastException:“对象必须实现IConvertible。”

我即将达到我目前的理解极限,感谢任何有关如何在终点线上实现这一目标的指针。

谢谢。

1 个答案:

答案 0 :(得分:0)

正如this解释的那样,Jama SaaS API最多允许返回50条记录,如果您想要超过50条记录,则必须进行多次调用。这可以通过简单的for循环来完成。

相关问题