Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Aug 24, 2011 01:49 PM by nayanancha
Member
158 Points
244 Posts
Aug 23, 2011 08:55 PM|LINK
Hi I have a JSON data, When I am trying to access a string value in my JSON object I am getting the below error:
Below is my code:
[Serializable] public class GetUser { public bool Status { get; set; } public string Messages { get; set; }//Getting error If I add this property of type string. public GetUserdata[] Data { get; set; } } [Serializable] public class GetUserdata { public string UserName { get; set; } public string FullName { get; set; } public string Email { get; set; } public string RoleName { get; set; } public string DivList { get; set; } public string RegList { get; set; } public string FacList { get; set; } }
Below is my JSON data:
{"Status":true,"ErrorCode":0,"Messages":["User has been added"], "Data": [ {"UserName":"TestUser", "Email":"testit@testit.com", "CompanyId":"999", "FullName":"Test User", "RoleName":"Division", "DivList":"div1,div2,div3", "RegList":"", "FacList":""}]}
My Get method:
string responseFromServer = WebRequests.Request.UserRequest(url, "GET"); JavaScriptSerializer serializer = new JavaScriptSerializer(); GetUser updateuser = serializer.Deserialize<GetUser>(JSONdata); model.status = updateuser.Status;model.UserName = updateuser.Data[0].UserName; model.FullName = updateuser.Data[0].FullName;I am able to get the status and other fields like Username, Fullname. But When I am trying to add Message of type string in my model. Its giving that error.How can I get the message Field in my JSON data. What changes do i need to make??Thanks,
All-Star
36644 Points
5432 Posts
Aug 23, 2011 09:56 PM|LINK
as the error message is trying to tell you, Messages is an array in the json object, and as such can not be converted to a string.
try:
public string[] Messages { get; set; }
Aug 24, 2011 01:49 PM|LINK
Ya. It works now.
nayanancha
Member
158 Points
244 Posts
Type 'System.String' is not supported for deserialization of an array?????
Aug 23, 2011 08:55 PM|LINK
Hi I have a JSON data, When I am trying to access a string value in my JSON object I am getting the below error:
Type 'System.String' is not supported for deserialization of an array
Below is my code:
Below is my JSON data:
{"Status":true,"ErrorCode":0,"Messages":["User has been added"],
"Data":
[
{"UserName":"TestUser",
"Email":"testit@testit.com",
"CompanyId":"999",
"FullName":"Test User",
"RoleName":"Division",
"DivList":"div1,div2,div3",
"RegList":"",
"FacList":""}]}
My Get method:
bruce (sqlwo...
All-Star
36644 Points
5432 Posts
Re: Type 'System.String' is not supported for deserialization of an array?????
Aug 23, 2011 09:56 PM|LINK
as the error message is trying to tell you, Messages is an array in the json object, and as such can not be converted to a string.
try:
public string[] Messages { get; set; }
nayanancha
Member
158 Points
244 Posts
Re: Type 'System.String' is not supported for deserialization of an array?????
Aug 24, 2011 01:49 PM|LINK
Ya. It works now.