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

Now*_*hin 2 c# arrays serialization json

我从维基百科生成了两个文本文件和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;
    }

    // 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图在一个数组中进行geocordinate.But根本不工作.

我的json文件看起来像这样

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

cra*_*opy 5

您的问题是您存储数据的格式.您目前拥有的不是 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"
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

你看到了区别?现在,当您使用所需的样本替换我的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; }
}
Run Code Online (Sandbox Code Playgroud)

这导致了这个json:

[
  {
    "ShortText": "ShortText41cca2ce-b139-458e-b20c-c549ba0e0163",
    "GeoCoordinates": {
      "Longitude": 10.13333333,
      "Latitude": 54.33333333
    },
    "Images": [
      "9B9C2047.jpg",
      "CAD72F59.jpg"
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

编辑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;
}
Run Code Online (Sandbox Code Playgroud)

用法:

this.GeoCoordinates = GeosFromString(startPath + @"\Latitude Longitude\" + name + ".json");
Run Code Online (Sandbox Code Playgroud)