Click here to Skip to main content
15,909,498 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
I am try to covert a c# object to json but am getting json obj in this way

"{\"PkUserId\":1,\"CubicalName\":\"21547\"}"

C#
System.Web.Script.Serialization.JavaScriptSerializer jSearializer =
                   new System.Web.Script.Serialization.JavaScriptSerializer();
            string list = jSearializer.Serialize(genricobj);


C#
public class UserProfile
    {
        [DataMember]
        public int PkUserId { set; get; }

        [DataMember]
        public string CubicalName { get; set; }
}


the problem is the above json is not a correct json format.

C#
List loginInfolist=new List();
if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
 foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                    
                    loginInfoDC = new UserProfile();
                    loginInfoDC.PkUserId = Convert.ToInt32(dr["pkUserId"]);
                    loginInfoDC.CubicalName = Convert.ToString(dr["CubicalName"]);
                    loginInfolist.add(loginInfoDC);
                } 

                }


i want to know how to serialise to json and how to differentiate it with list index
say list[0] json and the lit[1]json

meant all the serialised json of the list will be in same json obj how to differentiate
Posted
Updated 25-Jan-14 22:35pm
v3
Comments
Sampath Lokuge 26-Jan-14 3:41am    
Which format do you expect ?
naveend915 26-Jan-14 4:06am    
Json format like this

{"PkUserId":1,"CubicalName":"21547"}
Sampath Lokuge 26-Jan-14 4:08am    
Can you put your 'genricobj' code snippet also ?
naveend915 26-Jan-14 4:14am    
public class UserProfile
{
[DataMember]
public int PkUserId { set; get; }

[DataMember]
public string CubicalName { get; set; }
}
Sampath Lokuge 26-Jan-14 4:16am    
Not only this,the way you fill the data into 'genricobj' ?

1 solution

Microsoft's JavaScriptSerializer is not a Json serializer!!! So it will escape special chars...
Use Json.NET - much better...
http://james.newtonking.com/json[^]
 
Share this answer
 
Comments
Sampath Lokuge 26-Jan-14 5:00am    
+ 5 :)
Kornfeld Eliyahu Peter 26-Jan-14 5:02am    
Thanks...
naveend915 26-Jan-14 6:34am    
Can u please give an example how to do that?
naveend915 26-Jan-14 6:34am    
i have used JsonConvert.SerializeObject(result) , even then same issue so can u please give an example ?
Kornfeld Eliyahu Peter 26-Jan-14 6:39am    
1. there are over a 100 samples here:http://james.newtonking.com/json/help/index.html?topic=html/Samples.htm
2. are you looking at your result in the debug window?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900