Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse array of values - jQuery

Tags:

jquery

asp.net

Am calling a webservice from my jQuery to fetch both Staff and Student details. In my web service I have values for Staff as well as Students, and am returning these as array of strings. While returning values am just serializing these two as

[WebMethod(EnableSession = true)]
public string[] fetchStudentStaff(string sectionId)
{
string student;
string staff;
////
////
student = jsonSerialize.Serialize(studentList);
staff = jsonSerialize.Serialize(staffList);

return new string[] { student, staff };
}

here I've a variable called category for mentioning whether he is a staff or student. and am receiving this in my jQuery part as,

    [
    [
        [
            {
                "Name": "shanish",
                "StudentId": "12",
                "Category": "Student",
                "Mobile": "8147708287",
                "Email": "[email protected]"
            }
        ]
    ],
    [
        [
            {
                "Name": "shanish",
                "StaffId": "78",
                "Category": "Staff",
                "Mobile": "8147708287",
                "Email": "[email protected]"
            }
        ]
    ]
]

here, I tried using jQuery.parseJSON(data.d) to group the result, but it results null, I need to categorize the result with student and staff, here I have one student and one staff, for the case of 5 student and 2 staffs, I need to store students in a separate variable and 2 staffs in a separate variable.

I dunno how to achieve this, can anyone help me here...Thanks in advance

like image 423
shanish Avatar asked Nov 27 '25 14:11

shanish


1 Answers

I would send a structured message:

return jsonSerialize.Serialize(new {students=studentList, staff=staffList});

Then on the client:

$.ajax({url: urlToFetchStudentStaff, data: sectionId, dataType: 'json',
        success: function(result) {
            // assuming result.d is the JSON result message
            alert(result.d.students.length); // d.students is an array of students
            alert(result.d.staff.length); // d.staff is an array of staff
        });
like image 182
moribvndvs Avatar answered Nov 29 '25 07:11

moribvndvs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!