如何使用 C# 逐页解析 JSON URL

How to parse JSON URL Page by Page using C#

我是 c# 和 JSON 的新手。我正在尝试解析一个 JSON URL,它的请求限制为每页 100 objects。我无法检索超过 100 objects 第一页的页面,即使 21 页中有 2022 objects。

我的代码中缺少什么以允许解析 JSON objects 的所有 22 页?

提前感谢大家的帮助!

objects 的总计数由 HTTP 响应 header 给出:X-Total-Count

public class VCUtils
{
    public const string HeaderRateLimit = "X-Rate-Limit-Limit";
    public const string HeaderRateRemaining = "X-Rate-Limit-Remaining";
    public const string HeaderRateReset = "X-Rate-Limit-Reset";
    public const string HeaderCountTotal = "X-Total-Count";
}

以下是我的测试代码

 protected void Page_Load(object sender, EventArgs e)
{


    string baseURL = "https://api.veracross.com/vcdemo3/v2/students.json?page=";
    int recordcount = 1;
    int nextPage = 1;
    int totalObjectCount = 0;
    int defaultObjectsPerPage = 100;
    int pageCount = 1000;

    try
    {

        while (nextPage <= pageCount)  //pageCount
        {
            string jsonData = "";

            var req = (HttpWebRequest)WebRequest.Create(baseURL + nextPage);
            req.Credentials = new NetworkCredential(vcAPIUsername, vcAPIPassword);
            req.PreAuthenticate = true;
            HttpWebResponse response = req.GetResponse() as HttpWebResponse;

            using (var reader = new System.IO.StreamReader(response.GetResponseStream())) { jsonData = reader.ReadToEnd(); }
            // get total number of objects and calculate the page count
            if (response.Headers[VCUtils.HeaderCountTotal] != null)
            {
                totalObjectCount = Convert.ToInt16(response.Headers[VCUtils.HeaderCountTotal]);

                pageCount = (int)Math.Ceiling((double)totalObjectCount / (double)defaultObjectsPerPage);
            }


            VCStudents studentObj = new VCStudents();
            IList<VCStudents> validStudents;

            // Call the JSON.NET deserializer helper method
            validStudents = VCUtils.DeserializeToList<VCStudents>(jsonData);



            foreach (var item in validStudents)
            {
                var studs = item.GetType().GetProperties();
                //**

                int stkey = item.person_pk; //possibly legacy fields
                string surname = item.last_name;
                string first_name = item.first_name;
                string pref_name = item.preferred_name;
                string st_mobile = item.mobile_phone;
                string st_email = item.email_1;
                int gsis_id = item.person_pk;
                string birthdate = item.birthday;

                string gender = item.gender;
                string lastupdatedate = item.update_date;
                var fdkey = item.first_name; 


                Literal1.Text = Literal1.Text + recordcount + ") " + stkey + " " + surname + " " + first_name + " " + pref_name + " " + st_mobile + " " + st_email + " " + gsis_id + " " + birthdate + " " + roll_group + " " + gender + " " + fdkey + " " + lastupdatedate + "</br> ";

                recordcount = recordcount + 1;
            }

            nextPage = nextPage + 1;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
     finally
     {

     }
}

下面是引用自的JsonHelper方法,http://www.cshandler.com/2013/09/deserialize-list-of-json-objects-as.html#.WPguVUWGOJA

public static List<string> InvalidJsonElements;
public static IList<T> DeserializeToList<T>(string jsonString)
{
    InvalidJsonElements = null;
    var array = JArray.Parse(jsonString);
    IList<T> objectsList = new List<T>();

    foreach (var item in array)
    {
        try
        {
            // CorrectElements
            objectsList.Add(item.ToObject<T>());
        }
        catch (Exception ex)
        {
            InvalidJsonElements = InvalidJsonElements ?? new List<string>();
            InvalidJsonElements.Add(item.ToString());
        }
    }

    return objectsList;
}

以下是在json2csharp.com

上生成的class
 public class VCStudentRole
{
    public string description { get; set; }
}

public class VCStudents
{

    public const string studentsBasePath = "students";

    public int household_fk { get; set; }
    public int person_pk { get; set; }
    public string name_prefix { get; set; }
    public string first_name { get; set; }
    public string first_nick_name { get; set; }
    public string preferred_name { get; set; }
    public string middle_name { get; set; }
    public string last_name { get; set; }
    public string name_suffix { get; set; }
    public string birthday { get; set; }
    public string gender { get; set; }
    public string email_1 { get; set; }
    public bool display_email_1 { get; set; }
    public string email_2 { get; set; }
    public bool display_email_2 { get; set; }
    public string home_phone { get; set; }
    public bool display_home_phone { get; set; }
    public string mobile_phone { get; set; }
    public bool display_mobile_phone { get; set; }
    public int advisor_fk { get; set; }
    public string advisor_name { get; set; }
    public int homeroom_teacher_fk { get; set; }
    public string homeroom_teacher_name { get; set; }
    public int homeroom { get; set; }
    public List<VCStudentRole> roles { get; set; }
    public string current_grade { get; set; }
    public string grade_applying_for { get; set; }
    public int year_applying_for { get; set; }
    public bool resident_status_applying_for { get; set; }
    public bool student_group_applying_for { get; set; }
    public bool campus_applying_for { get; set; }
    public string school_level { get; set; }
    public string enrollment_status { get; set; }
    public bool new_student { get; set; }
    public int graduation_year { get; set; }
    public string resident_status { get; set; }
    public string campus { get; set; }
    public string dorm { get; set; }
    public string room_number { get; set; }
    public int floor_number { get; set; }
    public int bed_number { get; set; }
    public string mailbox_number { get; set; }
    public string student_group { get; set; }
    public int parent_1_fk { get; set; }
    public int parent_2_fk { get; set; }
    public int parent_3_fk { get; set; }
    public int parent_4_fk { get; set; }
    public string username { get; set; }
    public string update_date { get; set; }

}

我怀疑你用这段代码得到的总数是否准确:

totalObjectCount = Convert.ToInt16(response.Headers[VCUtils.HeaderCountTotal]);

你可以试试这个:

totalObjectCount = Convert.ToInt32(response.Headers.Get(VCUtils.HeaderCountTotal));

从 MSDN 查看更多详细信息:https://msdn.microsoft.com/en-us/library/system.net.webheadercollection(v=vs.110).aspx