使用 C# 以换行顺序写入 Json 文件
Write Json file with new line order using C#
我正在编写一个代码,我可以用它序列化一个 JSON 文件,其中包含多个数据,如短文本、地理坐标和图像。但是生成后会以大行显示。但是我想通过我的代码用新行 order.But 来组织它,但我无法生成它。我在这里使用的代码是-
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; }
}
现在我的 Class 我使用这个 类 的地方是
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);
}
尝试替换此代码:
string json = JsonConvert.SerializeObject(Poi);
有了这个:
string json = JsonConvert.SerializeObject(Poi, Formatting.Indented);
我正在编写一个代码,我可以用它序列化一个 JSON 文件,其中包含多个数据,如短文本、地理坐标和图像。但是生成后会以大行显示。但是我想通过我的代码用新行 order.But 来组织它,但我无法生成它。我在这里使用的代码是-
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; }
}
现在我的 Class 我使用这个 类 的地方是
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);
}
尝试替换此代码:
string json = JsonConvert.SerializeObject(Poi);
有了这个:
string json = JsonConvert.SerializeObject(Poi, Formatting.Indented);