繁体   English   中英

将 Json 字符串反序列化为用户定义的对象时出现异常

[英]Exception when deserializing a Json string to an user defined object

当我尝试将 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
    }
  ]
}

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

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; }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM