3

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

1 Answer 1

2

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
        });
Sign up to request clarification or add additional context in comments.

5 Comments

then how can I categorize in jquery
can u try alert(result). It will be expected to print the JSON data. Can u able to see it ?
See my updated answer. If you specify dataType:json, jQuery should automatically parse the resulting message as JSON, so you shoudl be able to use it directly.
@HackedByChinese: thanks dude, I hope ur solution will really help me, I'll give a try and let u know
@VeeKeyBee: the alert box not displaying the exact result, it displays result as [object],[object].....like this, its enough, I can achieve with this, thanks for ur support

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.