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

标签 c# arrays json serialization

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

Lat:54.33333333
Lon:10.13333333

我想将其放入数组中。 现在我用来为所有对象创建 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;
    }

    // This method will create json file for Neuschwanstein_Castle
    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");

        this.ShortText = File.ReadAllText(startPath + @"\Short Text\" + name+".json");
        this.GeoCoordinates = File.ReadAllLines(startPath + @"\Latitude Longitude\" + name + ".json");
        this.Images = new List<string> { Jpeg_File[0].Name, Jpeg_File[1].Name };

        string json = ToJson();
        File.WriteAllText(path, json);
    }
}

我正在尝试获取数组中的地理坐标。但根本不起作用。

我的 json 文件看起来像这样

[
    {
    "ShortText": "Kiel is the capital and most &tldr; from the wedge form of its bay ",
   "GeoCordinates": null,
    "Images": [
         "9B9C2047.jpg",
         "CAD72F59.jpg"
          ]
    }
]

最佳答案

您的问题是您存储数据的格式。您当前的格式是没有 json。您的类中的示例 json 如下所示:

[
  {
    "ShortText": "ShortText9daba9d8-1e0c-4b70-9f69-016332b1b53a",
    "GeoCordinates": [
      "11b2a991-0165-4155-8207-f256d2486ddd",
      "9abd9c64-f0f2-47fb-a692-c6daae15a5bc",
      "bfd2edd5-6f37-45ae-abf9-350cb20a5f5f"
    ],
    "Images": [
      "03425202-d953-44c5-bc50-bffdd4e7f605",
      "dd6d0a5f-8e93-4793-82b7-4aba3515993c",
      "8d554f56-af65-4ef3-b06a-88d2aab647e1"
    ]
  }
]

你看出区别了吗?现在,当您将我的 json 示例的内容替换为您想要的示例时,将其保存到文件中,然后您可以反序列化它。

写完这篇文章后,我很快又读了一遍你的问题,我有两个问题:

  • 您想解析您拥有的文本然后将其保存为 json 吗?
  • 您希望将数据存储在数组中。你在哪里有它?数组应该在哪里?

编辑:

你的类应该是这样的:

//using System.Collections.Generic;
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; }
}

结果是这个 json:

[
  {
    "ShortText": "ShortText41cca2ce-b139-458e-b20c-c549ba0e0163",
    "GeoCoordinates": {
      "Longitude": 10.13333333,
      "Latitude": 54.33333333
    },
    "Images": [
      "9B9C2047.jpg",
      "CAD72F59.jpg"
    ]
  }
]

编辑2:

public GeoCoordinates GeosFromString(string path)
{
    string[] lines = File.ReadAllLines(path);
    GoeCoordinates gc = new GeoCoordinates();
    gc.Latitude = Double.Parse(lines[0].Substring(4)); // this removes 'Lat:' in front of the string
    gc.Longitude = Double.Parse(lines[1].Substring(4)); // this removes 'Lon:' in front of the string
    return gc;
}

用法:

this.GeoCoordinates = GeosFromString(startPath + @"\Latitude Longitude\" + name + ".json");

关于c# - 使用 C# 从文本文件序列化 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34351779/

相关文章:

c# - 如何通过SqlConnection获取最后执行的SQL查询?

c# - 如何重构我的 LinqToSql 以使其更易于维护?

javascript - 使用 JavaScript 在数组中查找最近的日期

java - 从 Android 中的 JSONArray 中提取值

xml - 是否有将结构对象编码为 HTTP GET 请求参数的标准语法?

c# - 多次插入需要设置 Id 属性吗?

c# - WPF TPL 重新启动取消的任务

arrays - 获取数组中具有 Swift 中最低值的字典对象

arrays - 如何通过跳过一些数字将数字插入到循环数组中?

json - 在spark-streaming中解析json