0

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

harry.luson
  • 247
  • 7
  • 19
  • 1
    Please paste in what exception you get. – Szabolcs Dézsi Feb 09 '16 at 22:02
  • So you can't load some of the info into your properties? Does the input match the intended data type? In addition to the Exception info please also post some sample data (the content of a file that is failing) – AndreiROM Feb 09 '16 at 22:05
  • {System.FormatException: Input string was not in a correct format. at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt) at System.Double.Parse(String s) at WikiPerser.JSON_Output.ToJsonForLocation(String CityName, String PoiName) in c:\C# Visual Studio\TouristPlace-15-Jan-2016 For test\WikiPerser\Output\JSON_Output.cs:line 51 at TouristPlace.Form1.JSON_Click(Object sender, EventArgs e) in c:\C# Visu... – harry.luson Feb 09 '16 at 22:08
  • Sounds to me like you have some invalid input in your file. Try filtering your input for invalid characters and trimming them as well (removing empty spaces from the ends, etc.) – AndreiROM Feb 09 '16 at 22:09
  • @AndreiROM I mentioned my Latidute and Longitude File below my code from where I want to get the lines. – harry.luson Feb 09 '16 at 22:13

1 Answers1

0

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.

mojorisinify
  • 377
  • 5
  • 22
  • Instead of Double.Parse, use Double.TryParse() - it is safer and handles exception. – mojorisinify Feb 09 '16 at 22:16
  • No it is not working at all . I found the same problem@mojorisinify – harry.luson Feb 09 '16 at 22:18
  • I am pretty sure that the values you are getting from the text file are in an invalid format. You could do a Console.WriteLine on those values? @harry.luson – mojorisinify Feb 09 '16 at 22:22
  • now error is gone, But I got 0 for both Latitude and longitude "Longitude": 0.0, "Latitude": 0.0, – harry.luson Feb 09 '16 at 22:24
  • I added my text file below the question – harry.luson Feb 09 '16 at 22:26
  • @harry.luson There you go, now you know the issue. I think the file path might be incorrect and you are not getting any data at all. – mojorisinify Feb 09 '16 at 22:26
  • I got 2 lines in string [] lines. But cannot set tit to Latitude and Longitude. I also added how I want to get the final result.Could you please tell me how can I do that – harry.luson Feb 09 '16 at 22:28
  • Try renaming your Short Text and Latitude Longitude files without space between. @harry.luson – mojorisinify Feb 09 '16 at 22:30
  • but that is the Folder name. I store all external file inside the folder. and then read file according to the name of the folder and file. Like if I press Berlin it will store all data in the Folder and then read the files inside the folder while serialize – harry.luson Feb 09 '16 at 22:33
  • @harry.luson Check this out http://stackoverflow.com/questions/6521546/how-to-handle-spaces-in-file-path-if-the-folder-contains-the-space – mojorisinify Feb 09 '16 at 22:45
  • I added two links below my question which contain the image while I put break point. Could you please have a look. Also the problem is only in poi.Latidute and poi.Longitude. The Double.Tryparse already got value – harry.luson Feb 09 '16 at 22:55