Nowshin
Nowshin

Reputation: 67

Write Json file with new line order using C#

I am writing a code throug which I can serialize a JSON file with multiple data like shorttext,Geocoordinates, and image. But after generating it will show in a big line. But I want to organize it with new line order.But throug my code I cannot generated it. The Code I use here is-

public class GeoCoordinates
 {
    public double Longitude { get; set; }
    public double Latitude { get; set; }
 }

 public class POIData
 {
    public string Shorttext { get; set; }
    public GeoCoordinates GeoCoordinates { get; set; }
    public List<string> Images { get; set; }


 }

Now My Class where I use this classes is

   public GeoCoordinates GeosFromString(string path)
    {
        string[] lines = File.ReadAllLines(path);
        GeoCoordinates gc = new GeoCoordinates();
        gc.Latitude = Double.Parse(lines[0].Substring(4)); 
        gc.Longitude = Double.Parse(lines[1].Substring(4)); 
        return gc;
    }
    public void ToJsonForLocation(string name)
    {
        var startPath = Application.StartupPath;
        string folderName = Path.Combine(startPath, "Text_ImageWithHash");

        System.IO.Directory.CreateDirectory(folderName);

        string fileName = name + ".json";
        var path = Path.Combine(folderName, fileName);
        var Jpeg_File = new DirectoryInfo(startPath + @"\Image\" + name).GetFiles("*.jpg");

        POIData Poi=new POIData();
        Poi.Shorttext = File.ReadAllText(startPath + @"\Short Text\" + name + ".txt");
        Poi.GeoCoordinates=GeosFromString(startPath + @"\Latitude Longitude\" + name + ".txt");
        Poi.Images=new List<string> { Jpeg_File[0].Name};

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

    }

Upvotes: 1

Views: 15072

Answers (1)

clean_coding
clean_coding

Reputation: 1166

Try to replace this code:

string json = JsonConvert.SerializeObject(Poi);

With this:

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

Upvotes: 9

Related Questions