【问题标题】:Exception when deserializing a Json string to an user defined object将 Json 字符串反序列化为用户定义的对象时出现异常
【发布时间】:2020-04-25 22:58:34
【问题描述】:

在尝试将 Json 格式的字符串反序列化为用户定义的对象时,我遇到了在 Newtonsoft.Json.dll 中引发异常 Newtonsoft.Json.JsonSerializationException 的问题。我从文件中读取的字符串是在使用同一个对象之前写入的。

这是反序列化的代码(最后一行有异常)。

 storageFile   = await storageFolder.GetFileAsync(filePath);
 string JsonDB = await FileIO.ReadTextAsync(storageFile);

 MyDB myDB = Newtonsoft.Json.JsonConvert.DeserializeObject<MyDB>(JsonDB);

这是对象类(Tag 和 Astro 是枚举):

public class MyDB
{
    public string Version;
    public DateTime Date;
    public List<PhotoSpot_v0_1> Lista_v0_1;
}

public enum version
{
    v0_1
}

public class PhotoSpot_v0_1
{
    public int      ID          { get; set; }
    public string   Title       { get; set; }
    public string   Description { get; set; }
    public int      Rating      { get; set; }
    public bool     Reminder    { get; set; }
    public bool     Toast       { get; set; }

    private double   Latitude   { get; set; }
    private double   Longitude  { get; set; }
    private double   Altitude   { get; set; }

    public Geopoint Geopoint    { get; set; }
    public Geopoint Landmark    { get; set; }

    public TimeZoneInfo TimeZone { get; set; }

    public string MainTag { get; set; }


    public version Version { get; set; }
    public Landmark_v0_1             Landmarks    { get; set; }
    public List<Image_v0_1>          Images       { get; set; }
    public List<URL_v0_1>            URLs         { get; set; }
    public List<globalVars.Tag>      Tags         { get; set; }
    public List<Date_v0_1>           Dates        { get; set; }

    public bool Downloaded { get; set; }
    public bool Exportable { get; set; }
}

public class Landmark_v0_1
{
    public bool             freeLandmark  { get; set; }
    public string           Title         { get; set; }
    private double          Latitude      { get; set; }
    private double          Longitude     { get; set; }
    private double          Altitude      { get; set; }
    public globalVars.Astro Astro         { get; set; }
}

public class Image_v0_1
{
    public string imageURI { get; set; }
    public bool   isDownloaded { get; set; }
}

public class URL_v0_1
{
    public string sURL { get; set; }
}

public class Tag_v0_1
{
    //public string TagName { get; set; }
    public globalVars.TagImageURL TagName { get; set; }
}

public class Date_v0_1
{

    private double DateStart;
    private double DateEnd;

    public DateTime DateTimeStart()
    {
        return CoreTime.JDToDateTime(DateStart);
    }

    public DateTime DateTimeEnd()
    {
        return CoreTime.JDToDateTime(DateEnd);
    }

    public void SetDateTimeStart(DateTime dt)
    {
        DateStart = CoreTime.DateTimeToJD(dt);
    }

    public void SetDateTimeEnd(DateTime dt)
    {
        DateEnd = CoreTime.DateTimeToJD(dt);
    }


}

我检查过 Json 字符串在写入之前、写入之后和读取时是否相同。如果它有帮助,就在这里。如果需要更多内容,我会发布。

   {
  "Version": "v0_1",
  "Date": "2020-01-08T10:49:04.6992512+01:00",
  "Lista_v0_1": [
    {
      "ID": 2,
      "Title": "Abadi gaztelua",
      "Description": "",
      "Rating": 0,
      "Reminder": false,
      "Toast": false,
      "Geopoint": {
        "Position": {
          "Latitude": 43.380954170570355,
          "Longitude": -1.7520569699123154,
          "Altitude": 87.749645113013685
        },
        "AltitudeReferenceSystem": 2,
        "GeoshapeType": 0,
        "SpatialReferenceId": 4326
      },
      "Landmark": null,
      "TimeZone": {
        "Id": "Romance Standard Time",
        "DisplayName": "(UTC+01:00) Brussels, Copenhagen, Madrid, Paris",
        "StandardName": "Romance Standard Time",
        "DaylightName": "Romance Daylight Time",
        "BaseUtcOffset": "01:00:00",
        "SupportsDaylightSavingTime": true
      },
      "MainTag": "Monument",
      "Version": 0,
      "Landmarks": {
        "freeLandmark": false,
        "Title": null,
        "Astro": 0
      },
      "Images": [],
      "URLs": [],
      "Tags": [],
      "Dates": [],
      "Downloaded": false,
      "Exportable": true
    }
  ]
}

【问题讨论】:

  • MyDB 类的外观如何?你能分享一下MyDB的代码吗
  • 如果您没有向我们展示确切的异常消息或 MyDB 的样子,我们怎么能提供帮助?
  • 不知道MyDB类的结构,是不可能帮你的。另外,我们不应该在这里做跑腿工作。您可以通过尝试转换更简单的对象(即仅使用第一个字段,然后是前两个字段,然后...)来进一步调查 which 字段导致问题,直到您偶然发现可以'不被转换。我建议您至少知道哪个字段失败后再联系我们,这样您就可以提供MCVE 供我们使用。
  • @DavidG 我在“输出”窗口中收到的唯一错误/异常消息是我发布的消息:Newtonsoft.Json.dll 中的异常“Newtonsoft.Json.JsonSerializationException”我没有更多线索。 ..
  • 请花一些时间提出一个我们可以实际回答的问题。首先,这不是您的实际课程,因为它有错字并且无法编译。其次,你没有给我们所有的支持课程。第三,我再说一遍——你没有给我们完整的例外信息。例如,如果您使用您提供给我们的此代码,它将显示SerializationException: Member 'AdjustmentRules' was not found.

标签: c# .net json uwp


【解决方案1】:

我认为你的课不合适。我试过你的代码,它工作正常。

string filepath = "../../../json.txt";
            var test =  File.ReadAllText(filepath);
            Example myDB = JsonConvert.DeserializeObject<Example>(test);

请根据您的 json 检查您的以下课程

public class Position
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public double Altitude { get; set; }
    }

    public class Geopoint
    {
        public Position Position { get; set; }
        public int AltitudeReferenceSystem { get; set; }
        public int GeoshapeType { get; set; }
        public int SpatialReferenceId { get; set; }
    }

    public class TimeZone
    {
        public string Id { get; set; }
        public string DisplayName { get; set; }
        public string StandardName { get; set; }
        public string DaylightName { get; set; }
        public string BaseUtcOffset { get; set; }
        public bool SupportsDaylightSavingTime { get; set; }
    }

    public class Landmarks
    {
        public bool freeLandmark { get; set; }
        public object Title { get; set; }
        public int Astro { get; set; }
    }

    public class ListaV01
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public int Rating { get; set; }
        public bool Reminder { get; set; }
        public bool Toast { get; set; }
        public Geopoint Geopoint { get; set; }
        public object Landmark { get; set; }
        public TimeZone TimeZone { get; set; }
        public string MainTag { get; set; }
        public int Version { get; set; }
        public Landmarks Landmarks { get; set; }
        public IList<object> Images { get; set; }
        public IList<object> URLs { get; set; }
        public IList<object> Tags { get; set; }
        public IList<object> Dates { get; set; }
        public bool Downloaded { get; set; }
        public bool Exportable { get; set; }
    }

    public class MyDB
    {
        public string Version { get; set; }
        public DateTime Date { get; set; }
        public IList<ListaV01> Lista_v0_1 { get; set; }
    }

【讨论】:

  • MyDB 类及其子类就是它们。 TimeZone、Geopoint 和 Position 已经在 .NET 中定义,所以我不明白为什么需要再次对它们进行编码。 Landmark 也是一个子类,它不是您在那里提出的。
  • @ImanolZubiaurre ,你能为此分享你的命名空间吗?我会努力的。 b'cz 你的班级和 json 不匹配
  • 对于位置和地理点,我认​​为命名空间是 Windows.Devices.Geolocation。
  • 和时区信息?
  • "Version": 0 => 在你的类中它是在类下定义的..这个错误应该是整数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-22
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
  • 2014-01-04
  • 2020-02-03
相关资源
最近更新 更多