繁体   English   中英

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

[英]How to parse JSON URL Page by Page using C#

我是C#和JSON的新手。 我正在尝试解析一个JSON URL,该URL的请求限制为每页100个对象。 即使21页中有2022个对象,我也无法检索包含100个对象的第一页之外的页面。

我的代码中缺少哪些内容来允许解析所有22页的JSON对象?

谢谢您提前提供的所有帮助!

对象的总数由HTTP响应标头给出: 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
     {

     }
}

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

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上生成的类

 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/zh-cn/library/system.net.webheadercollection(v= vs.110) .aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM