【问题标题】:Json serialize from text file using c#使用c#从文本文件序列化Json
【发布时间】:2015-12-18 08:55:57
【问题描述】:

我从维基百科生成了两个文本文件和 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 不会解析 TEXT 文件...
  • 您需要使类可序列化。如果它是可序列化的,那么从提供的示例中并不明显。
  • @ToniKostelac 你能解释一下吗?我得到正确的短文本和图像,但不是地理坐标
  • 我认为@cramopy 在他的回答中很好地总结了这一点,但是我想补充一点,您通常希望使用 [Serializable] 属性将您的类标记为可序列化。我认为这可能有助于将文本解析为 double 或任何基本类型。

标签: c# arrays json serialization


【解决方案1】:

您的问题是您存储数据的格式。您目前拥有的是 no 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");

【讨论】:

  • 我已经编辑了我的问题。我在代码@cramopy 下提到了我的 json 文件
  • 我只有地理坐标序列化有问题
  • 现在问题出现在这里 - this.GeoCoordinates = File.ReadAllLines(startPath + @"\Latitude Longitude\" + name + ".json");线。它显示无法将类型'string []'隐式转换为'Wikipedia_Image_Text_24_11_2015.GeoCoordinates'
猜你喜欢
  • 1970-01-01
  • 2015-10-24
  • 2021-07-16
  • 1970-01-01
  • 2016-05-27
  • 2016-11-14
  • 2017-05-16
  • 1970-01-01
  • 2011-07-27
相关资源
最近更新 更多