将json sting反序列化为genric列表(将值转换为类型的错误...键入'System.Collections.Generic.List`1.Path' [英] deserialize json sting to genric list ( Error converting value to type... to type 'System.Collections.Generic.List`1 . Path '

查看:252
本文介绍了将json sting反序列化为genric列表(将值转换为类型的错误...键入'System.Collections.Generic.List`1.Path'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,



这里面临一点问题,同时将json字符串反序列化为列表



json string:restful service的结果

Hello friends,

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

json string : Result of restful service

"\"[{\\\"UName\\\":\\\"prasad\\\",\\\"LastName\\\":\\\"k\\\",\\\"FirstName\\\":\\\"sai\\\"}]\""



我想转换这个jso n字符串到_TempUser类列表



我的列表:


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

my list :

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

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;





代码:用于调用restful服务



code : for calling restful service

 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   
                }

            }





任何修改或建议PLZ?



any modifications or suggestions plz?

推荐答案

请参阅我对该问题的评论。您只有一个 _TempUser 的实例。它与 List 无关,不是以任何方式。这是序列化的示例: http://stackoverflow.com/questions/18711721 / jsonconvert-deserializeobjecttjsonstring-returning-all-propertiest-as-null [ ^ ]。



-SA
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


它是一个临时解决方案,如果有任何一次得到确切的解决方案请告诉我。



its an temporary solution, if any once got exact solution plz let me know.

 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>





它的工作正常对我来说;)



如果你wana检查你的json字符串有效或不只是使用这个链接



http://jsonlint.com/ [ ^ ]


你可以在javascript中使用escape()函数。这是你可以找到解决方案的链接转义json格式的特殊字符



http://stackoverflow.com/questions/21565404/jsonconvert-deserializeobject-special-characters-unterminated-string-expected-d [ ^ ]
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[^]


这篇关于将json sting反序列化为genric列表(将值转换为类型的错误...键入'System.Collections.Generic.List`1.Path'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆