looping through an object passed in via JQUERY as an array and using c# webmethod to get data out
all,
I'm getting to my webmethod in my code behind but I'm having problems deserializing my json data. I have no good reference but here is what I"m trying to do. My the code in my webmethod is not allowing me to get the data out passed from my ajax call. thanks for any help.
$("[id$=rdbSaveAjax1]").click(function () {
var mappedJobRole = new Array();
$(".jobRole").each(function (index) {
var jobRoleIndex = index;
var jobRoleID = $(this).attr('id');
var jobRoleName = $(this).text();
// add all the roleids and rolenames to the job role array.
var roleInfo = {
"roleIndex": jobRoleIndex,
"roleID": jobRoleID,
"roleName": jobRoleName
};
queryStr = { "roleInfo": roleInfo };
mappedJobRole.push(queryStr);
});
$.ajax({
type: "POST",
url: "Apage.aspx/Save_Mapped_Role",
data: "{'savedRole': " + JSON.stringify(mappedJobRole) + "}",
contentType: "application/json; charset=u开发者_Go百科tf-8",
dataType: "json",
async: false,
success: function (data) {
alert("successfully posted data");
},
error: function (data) {
alert("failed posted data");
}
});
});
In my code behind I can't seem to get the data out. My class:
public class MappedRole
{
public int Index { get; set; }
public string RoleID { get; set; }
public string RoleName { get; set; }
}
My webmethod:
[WebMethod]
public static bool Save_Mapped_Role(object savedRole)
{
bool success = false;
JavaScriptSerializer js = new JavaScriptSerializer();
IList<MappedRole> role = new JavaScriptSerializer().Deserialize<IList<MappedRole>>savedRole);
int Index = role[0].Index;
string RoleID = role[0].RoleID;
string RoleName = role[0].RoleName;
return success;
}
This line seems to be missing an opening parenthese
IList<MappedRole> role = new JavaScriptSerializer().Deserialize<IList<MappedRole>>savedRole);
Should read
IList<MappedRole> role = new JavaScriptSerializer().Deserialize<IList<MappedRole>>(savedRole);
Furthermore, in order to use the Deserialize method, you need to create classes based on the JSON variables you are passing. For example based on the JSON object you created, you should have the two classes below.
SavedRole Class
public class SavedRole
{
public roleInfo[] { get; set; }
}
roleInfo Class
public class roleInfo
{
public int roleIndex { get; set; }
public string roleID { get; set; }
public string roleName { get; set; }
}
Now the Deserialze method will do its magic and populate the objects for you. Then you'll be able to loop through the object and do what you need with the data.
[WebMethod]
public static bool Save_Mapped_Role(object savedRole)
{
bool success = false;
var serializer = new JavaScriptSerializer();
SavedRole role = serializer.Deserialize<SavedRole>(savedRole);
//Loop through the data like so
int roleIndex = 0;
string roleID = null;
string roleName = null;
foreach (var item in role.roleInfo) {
roleIndex = item.roleIndex;
roleID = item.roleID;
roleName = item.roleName;
//Do more logic with captured data
}
return success;
}
Hope that helps
this post explain how you can convert a JSON to C#: Parse JSON in C#
If you don't want to use that, you need to do some changes in your project:
First, to get the your RoleInfo, you need to transform it in a Dictionary like:
(((object[])savedRole)[0] as Dictionary<string, object>)["roleInfo"]
After that, you can manipule your object to create your List:
var list = ((object[])savedRole);
IList<MappedRole> role = new List<MappedRole>();
foreach (var item in list)
{
var dic = ((item as Dictionary<string, object>)["roleInfo"] as Dictionary<string, object>);
MappedRole map = new MappedRole()
{
roleIndex = Convert.ToInt32(dic["roleIndex"]),
roleID = dic["roleID"].ToString(),
roleName = dic["roleName"].ToString()
};
role.Add(map);
}
精彩评论