小编Now*_*hin的帖子

Json使用c#从文本文件序列化

我从维基百科生成了两个文本文件和jpeg文件.现在我想以json格式序列化所有这个文件.第一个我生成了一个短文本文件,第二个我生成了该位置的纬度和经度,第三个是图像文件作为哈希码.图像和短文文件工作正常.但我有纬度和经度文件的问题.我的文本文件看起来像是新行

Lat:54.33333333
Lon:10.13333333
Run Code Online (Sandbox Code Playgroud)

我想把它放在一个数组中.现在我用于为所有对象创建json的json类是 -

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

    public string ToJson()
    {
        string json = JsonConvert.SerializeObject(this, Formatting.Indented);
        return json;
    }

    public void FromJson(string json)
    {
        dynamic poi = JsonConvert.DeserializeObject<POIData>(json);
        ShortText = poi.ShortText;
        Images = poi.Images;
        GeoCoordinates = poi.GeoCordinates;
    } …
Run Code Online (Sandbox Code Playgroud)

c# arrays serialization json

2
推荐指数
1
解决办法
1683
查看次数

使用 C# 以新行顺序编写 Json 文件

我正在编写一个代码,通过它我可以序列化包含多个数据(如短文本、地理坐标和图像)的 JSON 文件。但生成后会显示一条大线。但我想用新的行顺序组织它。但是通过我的代码我无法生成它。我在这里使用的代码是-

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


 }
Run Code Online (Sandbox Code Playgroud)

现在我使用这个类的类是

   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 = …
Run Code Online (Sandbox Code Playgroud)

c# serialization json newline json.net

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×2

json ×2

serialization ×2

arrays ×1

json.net ×1

newline ×1