C# Json序列化在使用c进行双重解析时不使用字符串行#

C# Json序列化在使用c进行双重解析时不使用字符串行#,c#,json,serialization,json.net,double,C#,Json,Serialization,Json.net,Double,我想从本地文件中的内容序列化json对象 这些文件包含:名称、短文本、纬度、经度和图像 当我运行代码字符串行时,它可以从文件中读取行,但不能设置纬度和经度属性。将引发异常 以下是我的课程和代码: public class POIData { public string Name { get; set; } public string Shorttext { get; set; } public double Longitude { get; set; } publ

我想从本地文件中的内容序列化json对象

这些文件包含:名称、短文本、纬度、经度和图像

当我运行代码字符串行时,它可以从文件中读取行,但不能设置纬度和经度属性。将引发异常

以下是我的课程和代码:

public class POIData
{
    public string Name { get; set; }
    public string Shorttext { get; set; }
    public double Longitude { get; set; }
    public double Latitude { get; set; }
    public string Image { get; set; }
 }
Json序列化方法是

 public void ToJsonForLocation(string CityName,string PoiName)
    {
        var startPath = Application.StartupPath;
        string folderName = Path.Combine(startPath, "FinalJson");
        string SubfolderName = Path.Combine(folderName, CityName);
        System.IO.Directory.CreateDirectory(SubfolderName);
        string fileName = PoiName + ".json";

        var path = Path.Combine(SubfolderName, fileName);
        var Jpeg_File = new DirectoryInfo(startPath + @"\Image\" + PoiName).GetFiles("*.jpg");

        POIData Poi=new POIData();
        Poi.Name = PoiName;
        Poi.Shorttext = File.ReadAllText(startPath + @"\Short Text\" + PoiName + ".txt");
        string[] lines = File.ReadAllLines(startPath + @"\Latitude Longitude\" + PoiName + ".txt"); //in this line 2 lines occured while entering breakpoint
        Poi.Latitude = Double.Parse(lines[0].Substring(4)); //show error cannot get any data
        Poi.Longitude = Double.Parse(lines[1].Substring(4));  //show error cannot get any data
        Poi.Image=Jpeg_File[0].Name;

        string json = JsonConvert.SerializeObject(Poi,Formatting.Indented);
        File.WriteAllText(path,json);


  }
我的经纬度的.txt文件是-

  Latitude:54.79541778
  Longitude:9.43004861
我想用这种方式序列化json-

  {
    "Name": "Flensburg Firth",
    "Shorttext": "Flensburg Firth or Flensborg Fjord  is the westernmost inlet of the Baltic Sea. It forms part of the border between Germany to the south and Denmark to the north. Its....",
    "Longitude": 9.42901993,
    "Latitude": 54.7959404,
    "Image": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8c/Flensborg_Fjord_ved_bockholmwik.jpg/400px-Flensborg_Fjord_ved_bockholmwik.jpg"
    }


问题似乎出在这两方面

 Poi.Latitude = Double.Parse(lines[0].Substring(4));  
 Poi.Longitude = Double.Parse(lines[1].Substring(4));  
确保添加以下空值和长度检查

    if(!string.IsNullOrEmpty(lines[0]) && lines[0].Length >= 5)
    {
       double dd ;
       Double.TryParse(lines[0].Substring(4), out dd)
       Poi.Latitude = dd; 
    }

    if(!string.IsNullOrEmpty(lines[1]) && lines[1].Length >= 5) 
    {
       double dd ;
       Double.TryParse(lines[1].Substring(4), out dd)
       Poi.Longitude = dd; 
     }

看看这是否有帮助。

请粘贴您得到的异常。这样您就无法将某些信息加载到您的属性中了?输入是否与预期的数据类型匹配?除了异常信息之外,还请在WikiPerser.JSON\u Output.ToJsonForLocation的System.Double.ParseDouble(字符串值、NumberStyles选项、NumberFormatInfo numfmt)上发布一些示例数据(失败文件的内容){System.FormatException:输入字符串的格式不正确c:\c\Visual Studio\TouristPlace-15-Jan-2016的test\WikiPerser\Output\JSON\u Output.cs:TouristPlace.Form1.JSON\u单击c:\c\Visu中的(Object sender,EventArgs e)在我听来好像文件中有一些无效输入。尝试过滤输入的无效字符并对其进行修剪(从末尾删除空格等)@AndreiROM我在代码下面提到了我的Latidute和Longitude文件,我想从中获取行。使用Double.TryParse()代替Double.Parse-它更安全,可以处理异常。不,它根本不工作。我发现了相同的问题problem@mojorisinifyI我非常确定您从文本文件中获取的值的格式无效。您可以对这些值执行Console.WriteLine?@harry.lusonnow错误消失,但我得到的纬度和经度“经度”均为0:0.0,“纬度”:0.0,我在问题下方添加了文本文件