首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Newtonsoft.Json反序列化失败

Newtonsoft.Json反序列化失败
EN

Stack Overflow用户
提问于 2019-02-28 00:41:04
回答 1查看 213关注 0票数 0

重构一个类之后,用

代码语言:javascript
复制
file.Write(JsonConvert.SerializeObject(this, Formatting.Indented));

按照预期工作,并生成本期中描述的json文件。但是,反序列化不再有效。

http://json2csharp.com能够正确地检索对象结构。

源/目标类型

代码语言:javascript
复制
 public class VinciModel
    {
        public string Name { get; set; }
        public string VidiWorkspaceDir { get; set; }
        public string ViDiWorkspaceName { get; set; }

        public int NRows { get; set; }
        public List<List<object>> Rows { get; set; }
        public int NChocolates { get; set; }
        public List<StreamInfo> StreamList { get; set; }
        public float BlisterWidth { get; set; }
        public float BlisterHeight { get; set; }
    }

    public class Socket
    {
        public int Index { get; set; }
        public string StreamName { get; set; }
        public Rectangle ROI { get; set; }
    }

    public class StreamInfo
    {
        public string StreamName { get; set; }
        public List<Socket> Sockets { get; set; } = new List<Socket>();
    }
}

源/目标JSON

代码语言:javascript
复制
{
  "Name": "sdgsd",
  "VidiWorkspaceDir": "D:\\VidiProjects\\VidiWorkspace-demo",
  "ViDiWorkspaceName": "box2shrink_2",
  "NRows": 1,
  "Rows": [
    [
      {
        "Index": 0,
        "StreamName": "mystream",
        "ROI": "0, 0, 5, 51"
      },
      {
        "Index": 0,
        "StreamName": "1",
        "ROI": "5, 0, 5, 51"
      },
      {
        "Index": 0,
        "StreamName": "2",
        "ROI": "11, 0, 5, 51"
      },
      {
        "Index": 0,
        "StreamName": "mystream",
        "ROI": "17, 0, 5, 51"
      }
    ]
  ],
  "NChocolates": 4,
  "StreamList": [
    {
      "StreamName": "mystream",
      "Sockets": [
        {
          "Index": 0,
          "StreamName": "mystream",
          "ROI": "0, 0, 5, 51"
        },
        {
          "Index": 0,
          "StreamName": "mystream",
          "ROI": "17, 0, 5, 51"
        }
      ]
    },
    {
      "StreamName": "1",
      "Sockets": [
        {
          "Index": 0,
          "StreamName": "1",
          "ROI": "5, 0, 5, 51"
        }
      ]
    },
    {
      "StreamName": "2",
      "Sockets": [
        {
          "Index": 0,
          "StreamName": "2",
          "ROI": "11, 0, 5, 51"
        }
      ]
    }
  ],
  "BlisterWidth": 23.0,
  "BlisterHeight": 51.0
}

错误

Newtonsoft.Json.JsonReaderException:‘分析值时遇到意外字符:{。路径’行‘,第8行,位置7。’

重现步骤

代码语言:javascript
复制
using (var file = new System.IO.StreamReader(path))
    JsonConvert.DeserializeObject<VinciModel>(file.ReadToEnd());

在类被重构之前工作

我也试过了:

代码语言:javascript
复制
var obj = JsonConvert.DeserializeObject<JObject>(file.ReadToEnd());

并得到:“读取字符串时出错。意外的标记: StartObject。路径为‘Rows’。”

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-28 01:20:28

问题是我的类有一个非默认构造函数,没有默认构造函数。错误消息无关紧要。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54910364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档