harry.luson
harry.luson

Reputation: 247

Json serialize not working with string line in Double parsing using c#

I want to serialize a json object from content in a local file.

These files contain: Name, Shorttext, Latitude, Longitude and Image.

When I run my code string line can read the lines from file but it cannot set the Latitude and Longitude properties. An exception is thrown.

The following are my class and code:

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

And the Json serialization method is

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


  }

my .txt file for latitude longitude is-

  Latitude:54.79541778
  Longitude:9.43004861

I want to serialize json in this way-

  {
    "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"
    }

Double try parse line got value

this does not get any value

Upvotes: 0

Views: 669

Answers (1)

mojorisinify
mojorisinify

Reputation: 405

Seems like the problem is along these two lines

 Poi.Latitude = Double.Parse(lines[0].Substring(4));  
 Poi.Longitude = Double.Parse(lines[1].Substring(4));  

Make sure to add following checks for null and length

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

See if that helps.

Upvotes: 0

Related Questions