【问题标题】:Getting error when trying to parse JSON Response - Cannot convert object of type 'System.String' to type ''尝试解析 JSON 响应时出错 - 无法将“System.String”类型的对象转换为“”类型
【发布时间】:2018-02-20 05:04:19
【问题描述】:

尝试解析 JSON 响应时出错

try
{
   var result = (new JavaScriptSerializer()).Deserialize<Rootobject>(jsonResponse);
}
catch(Exception ex){}

JSON 字符串

"[{\"ID\":1,\"Code\":null,\"Name\":\"Black\"},{\"ID\":1,\"Code\":null,\"Name\":\"Red\"},{\"ID\":1,\"Code\":\"blx\",\"Name\":\"Blue\"}]"

附上错误详情和准确 JSON 字符串的屏幕截图

我正在使用以下代码生成 JSON

   public string JSONTest()
   {
        List<Color> colors = new List<Color>(); 
        colors.Add(new Color() { ID = 1, Name = "Black" }); 
        colors.Add(new Color() { ID = 1, Name = "Red" }); 
        colors.Add(new Color() { ID = 1, Name = "Blue", Code = "blx" }); 

        return JsonConvert.SerializeObject(colors); 
    }

【问题讨论】:

  • 我也尝试过 Class1 而不是 RootObject 但同样的错误。(您可以在附加的屏幕截图中看到 Class1 和 RootObject 类。)我在jsonlint.com 中验证的 JSON 字符串是有效的。
  • 你的 json 被编码了两次。
  • 你需要修改你的json字符串为 { "Property1" : [ ] }
  • 你展示的json可以序列化为IEnumerable&lt;Class1&gt;(不是Rootobject
  • @StephenMuecke - {“无法将'System.String'类型的对象转换为'System.Collections.Generic.IEnumerable`1[Class1]'”}

标签: c# asp.net json asp.net-mvc jsonparser


【解决方案1】:

试试这个

var result = (new JavaScriptSerializer()).Deserialize<List<Class1>>(jsonResponse);

您的结果将是 Class1 的列表

【讨论】:

  • {"无法将'System.String'类型的对象转换为'System.Collections.Generic.List`1[Class1]'类型的对象"}
  • 我尝试了 LinqPad 中的代码,它能够反序列化 json 字符串。
  • 代码出错。我想问题出在我的 json 字符串上。我猜编码了两次。
  • 是的,您的图像显示的是双编码字符串。您需要检查生成 json 的函数和解析器函数之间的原因。
  • 使用 (WebClient client = new WebClient()) { string jsonResponse = client.DownloadString(colorUrl); JSONParseTest(jsonResponse); }
【解决方案2】:

嘿试试下面的:

 Class1 obj=new Class1();
 obj=serializer.Deserialize<Class1>(jsonResponse);

【讨论】:

    【解决方案3】:

    JSON string反序列化需要Root Key来完成反序列化过程

    在你的情况下,这个 JSON 不知道要反序列化哪个对象

    您必须显示反序列化 JSON 字符串中的类..

     public class Rootobject
     {         
        public Class1[] Property1 { get; set; }
     }
    
    
     public class Class1
     {
        public int ID { get; set; }
    
        public string Code { get; set; }
    
        public string Name { get; set; }
     }
    
    
    
         string jsonResponse = "{ \"Property1\" :  [{\"ID\":1,\"Code\":null,\"Name\":\"Black\"},{\"ID\":1,\"Code\":null,\"Name\":\"Red\"},{\"ID\":1,\"Code\":\"blx\",\"Name\":\"Blue\"}] }";
    
         var result = (new JavaScriptSerializer()).Deserialize<Rootobject>(jsonResponse);
    

    使用根密钥生成 JSON 字符串

        public string JSONTest()
        {
    
            List<Class1> colors = new List<Class1>();
            colors.Add(new Class1() { ID = 1, Name = "Black" });
            colors.Add(new Class1() { ID = 1, Name = "Red" });
            colors.Add(new Class1() { ID = 1, Name = "Blue", Code = "blx" });
    
            return JsonConvert.SerializeObject(new Rootobject { Property1 = colors.ToArray() });
        }
    

    或者,

    DeserializeObject 方法可以直接在object 上反序列化。 如果你像这样使用它,你将不需要根键但返回值将是一个对象..

     string jsonResponse = "[{\"ID\":1,\"Code\":null,\"Name\":\"Black\"},{\"ID\":1,\"Code\":null,\"Name\":\"Red\"},{\"ID\":1,\"Code\":\"blx\",\"Name\":\"Blue\"}]";
                    object[] result = (object[])(new JavaScriptSerializer()).DeserializeObject(jsonResponse);
    

    【讨论】:

    • 您的代码运行良好。如何在 JSON 字符串中附加“Property1”。不要求附加 json 字符串。询问 json 从哪里生成(您可以在上面的问题末尾看到 JSON 字符串生成代码)。
    • @Alex 我更新了答案,添加了 Generate JSON string with Root Key
    猜你喜欢
    • 2012-09-07
    • 2014-03-14
    • 2017-11-24
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 2021-04-26
    相关资源
    最近更新 更多