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

5
  • 1
    Please paste in what exception you get. Commented Feb 9, 2016 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) Commented Feb 9, 2016 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... Commented Feb 9, 2016 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.) Commented Feb 9, 2016 at 22:09
  • @AndreiROM I mentioned my Latidute and Longitude File below my code from where I want to get the lines. Commented Feb 9, 2016 at 22:13

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

Instead of Double.Parse, use Double.TryParse() - it is safer and handles exception.
No it is not working at all . I found the same problem@mojorisinify
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
now error is gone, But I got 0 for both Latitude and longitude "Longitude": 0.0, "Latitude": 0.0,
I added my text file below the question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.