Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hello friends,

here am facing little bit problem while deserializing json string to list

json string : Result of restful service
C#
"\"[{\\\"UName\\\":\\\"prasad\\\",\\\"LastName\\\":\\\"k\\\",\\\"FirstName\\\":\\\"sai\\\"}]\""

and i want to convert this json string to list of "_TempUser" class

my list :
C#
public class _TempUser
      {

          public string UName
          {
              get;
              set;

          }


          public string LastName
          {
              get;
              set;
          }

          public string FirstName
          {
              get;
              set;
          }
      }


Error is
Error converting value "[{"UName":"prasad","LastName":"k","FirstName":"sai"}]" to type 'System.Collections.Generic.List`1[loginServices.Login_Service+_TempUser]'. Path '', line 1, position 70.


code in my restful service
C#
List<_TempUser> List = new List<_TempUser>();

_TempUser ud = new _TempUser();

           ud.UName = "prasad";
           ud.FirstName = "sai";
           ud.LastName = "k";
           List.Add(ud);

           string json = JsonConvert.SerializeObject(List);

           return json;


code : for calling restful service
C#
 var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://IpAddress:Post/Login/RestServiceName.svc/RestMethoName");
            httpWebRequest.ContentType = "text/json";
            httpWebRequest.Method = "POST";
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json =JsonConvert.SerializeObject(Req);
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    string result =  streamReader.ReadToEnd();
                  
// here am converting json string to "_TempUser" List 

                    List<_TempUser> List = new List<_TempUser>();  

                    List = JsonConvert.DeserializeObject<List<_TempUser>>result   
                }

            }


any modifications or suggestions plz?
Posted
Updated 22-Jan-18 6:16am
v10
Comments
Sergey Alexandrovich Kryukov 25-Nov-13 2:19am    
Where do you see a list? This is a Javascript associative container (all Javascript arrays are like that). One such object.
—SA
Lokesh Kondapalli 25-Nov-13 2:25am    
Hello , really sorry i dint get you here my simple question

am receiving json string like this "\"{\\\"UName\\\":\\\"prasad\\\",\\\"LastName\\\":\\\"k\\\",\\\"FirstName\\\":\\\"sai\\\"}\""

and i want to convert this string to my list : "list<_TempUser>"

is it possible ?
Sergey Alexandrovich Kryukov 25-Nov-13 2:39am    
Wrong question. There is no a list. If you need a list, make it. Deserialize it to _TempUser, make a list (why? why?!) with this only element.
—SA
Lokesh Kondapalli 25-Nov-13 2:30am    
am returning from my restful service like this

List<_TempUser> List = new List<_TempUser>();
List.Add(ud);

string json = JsonConvert.SerializeObject(List);

return json;

with the help of newtonsoft json dll at server and client side for serialization and deserialization

Validate the type of data that you are sending in the header I am referring to MediaTypeWithQualityHeaderValue ("text / plain") <- since it can be an "application / Json" or "text / json", this type can change the serialization.

In my case I changed it to text / plain and it worked

I hope to be helpful
 
Share this answer
 
Comments
an0ther1 22-Jan-18 15:30pm    
This question was raised 4 years ago & has an accepted answer.
Answering old questions will lead to down votes.

Kind Regards
U can just use the escape() function in the javascript.This is link where u can find the solution for escaping the special characters in the json format

http://stackoverflow.com/questions/21565404/jsonconvert-deserializeobject-special-characters-unterminated-string-expected-d[^]
 
Share this answer
 
its an temporary solution, if any once got exact solution plz let me know.

C#
 using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    string result =  streamReader.ReadToEnd();
                  
// here am converting json string to "_TempUser" List 

 // before that am removing special characters 
  //  string is "\"[{\\\"UName\\\":\\\"prasad\\\",\\\"LastName\\\":\\\"k\\\",\\\"FirstName\\\":\\\"sai\\\"}]\""

  string s = result.Replace(@"\", string.Empty);

// after this   string is 
//  "[{"UName":"sravi","LastName":"last","FirstName":"firest"}]"

                  string final= s.Trim().Substring(1,(s.Length)-2);

// after this   string is 
//  [{"UName":"sravi","LastName":"last","FirstName":"firest"}]

                    List<_TempUser> List = new List<_TempUser>();  
 
                    List = JsonConvert.DeserializeObject<list><_tempuser>>(final)
               }
</_tempuser></list>


its working fine for me ;)

if u wana check ur json string valid or not just use this link

http://jsonlint.com/[^]
 
Share this answer
 
Please see my comment to the question. You have just one instance of _TempUser. It has nothing to do with List, not in any way. This is the example of serialization: http://stackoverflow.com/questions/18711721/jsonconvert-deserializeobjecttjsonstring-returning-all-propertiest-as-null[^].

—SA
 
Share this answer
 
Comments
Lokesh Kondapalli 25-Nov-13 2:35am    
really sorry for that ,am trying with class and list... am updated my question so can see any possibilities for this list

"\"[{\\\"UName\\\":\\\"prasad\\\",\\\"LastName\\\":\\\"k\\\",\\\"FirstName\\\":\\\"sai\\\"}]\""
Sergey Alexandrovich Kryukov 25-Nov-13 2:38am    
"Do or do not. There is no try". Why are you repeating this string? I already saw it. My answer should explain you everything. What else is unclear?
—SA
Lokesh Kondapalli 25-Nov-13 2:44am    
by seeing ur answer i only understand that... "JSON do not match the property names" but its entirely different here am having Exact name in service as well as in client side so how can i fix this by only seeing ur link ? is it correct link for my question ?
Sergey Alexandrovich Kryukov 25-Nov-13 2:50am    
What are you talking about?! Maybe you saw someone else's answer? :-)
I cannot understand what are you missing, probably something very basic...
A link? The sample corresponds almost one-to-one to what you should have. What could be unclear?
—SA
Lokesh Kondapalli 25-Nov-13 2:59am    
http://stackoverflow.com/questions/18711721/jsonconvert-deserializeobjecttjsonstring-returning-all-propertiest-as-null

i think its the exact link what you posted for my question.
in url link i seen the answer that "JSON do not match the property names in your class"
but its not in my case .


yes am missing some thing basic and Error occurring, due to those special characters.

and with out those characters its working fine
that string is [{"UName":"prasad","LastName":"last","FirstName":"firest"}] i checked in
http://www.jsoneditoronline.org/

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