winforms 如何使用json从文本文件反序列化回字典〈string,string>?

2w2cym1i  于 2022-11-17  发布在  其他
关注(0)|答案(2)|浏览(167)

我使用的是Newtonsoft.Json包。
在表单顶部:

Dictionary<string, string> FileList = new Dictionary<string, string>();

这就是我在每次添加到json文本文件时,首先在mouse up事件中将其序列化为文本文件的方法:

rectangleName = @"d:\Rectangles\rectangle" + saveRectanglesCounter + ".bmp";
                        FileList.Add($"{dr.Location}, {dr.Size}", rectangleName);
                        string json = JsonConvert.SerializeObject(
    FileList,
    Formatting.Indented // this for pretty print
);
                        using (StreamWriter sw = new StreamWriter(@"d:\rectangles.txt", true))
                        {
                            sw.Write(json);
                            sw.Close();
                        }

然后在构造函数中

FileList = JsonConvert.DeserializeObject<Dictionary<string, string>>(@"d:\rectangles.txt");

但是我在那行上得到错误:
Newtonsoft.Json.JsonReaderException:'分析值时遇到意外字符:d.路径“”,第0行,位置0 '
我尝试了这个方法,首先将文件内容读取到字符串变量,然后反序列化字符串变量:

string g = File.ReadAllText(@"d:\rectangles.txt");
FileList = JsonConvert.DeserializeObject<Dictionary<string, string>>(g);

但现在获取错误:
HResult= 0x 80131500消息=阅读JSON内容后遇到其他文本:{.路径“”,第3行,位置1。源代码=Newtonsoft.Json
这是文件内容,而不是g变量内容,而是文件:

"{X=79,Y=117}, {Width=278, Height=119}": "d:\\Rectangles\\rectangle1.bmp"
}{
  "{X=79,Y=117}, {Width=278, Height=119}": "d:\\Rectangles\\rectangle1.bmp",
  "{X=75,Y=101}, {Width=412, Height=195}": "d:\\Rectangles\\rectangle2.bmp"
}{
  "{X=79,Y=117}, {Width=278, Height=119}": "d:\\Rectangles\\rectangle1.bmp",
  "{X=75,Y=101}, {Width=412, Height=195}": "d:\\Rectangles\\rectangle2.bmp",
  "{X=176,Y=256}, {Width=120, Height=122}": "d:\\Rectangles\\rectangle3.bmp"
}{
  "{X=79,Y=117}, {Width=278, Height=119}": "d:\\Rectangles\\rectangle1.bmp",
  "{X=75,Y=101}, {Width=412, Height=195}": "d:\\Rectangles\\rectangle2.bmp",
  "{X=176,Y=256}, {Width=120, Height=122}": "d:\\Rectangles\\rectangle3.bmp",
  "{X=202,Y=240}, {Width=24, Height=16}": "d:\\Rectangles\\rectangle4.bmp"
}{
  "{X=79,Y=117}, {Width=278, Height=119}": "d:\\Rectangles\\rectangle1.bmp",
  "{X=75,Y=101}, {Width=412, Height=195}": "d:\\Rectangles\\rectangle2.bmp",
  "{X=176,Y=256}, {Width=120, Height=122}": "d:\\Rectangles\\rectangle3.bmp",
  "{X=202,Y=240}, {Width=24, Height=16}": "d:\\Rectangles\\rectangle4.bmp",
  "{X=190,Y=98}, {Width=78, Height=190}": "d:\\Rectangles\\rectangle5.bmp"
}{
  "{X=79,Y=117}, {Width=278, Height=119}": "d:\\Rectangles\\rectangle1.bmp",
  "{X=75,Y=101}, {Width=412, Height=195}": "d:\\Rectangles\\rectangle2.bmp",
  "{X=176,Y=256}, {Width=120, Height=122}": "d:\\Rectangles\\rectangle3.bmp",
  "{X=202,Y=240}, {Width=24, Height=16}": "d:\\Rectangles\\rectangle4.bmp",
  "{X=190,Y=98}, {Width=78, Height=190}": "d:\\Rectangles\\rectangle5.bmp",
  "{X=231,Y=86}, {Width=128, Height=174}": "d:\\Rectangles\\rectangle6.bmp"
}{
  "{X=79,Y=117}, {Width=278, Height=119}": "d:\\Rectangles\\rectangle1.bmp",
  "{X=75,Y=101}, {Width=412, Height=195}": "d:\\Rectangles\\rectangle2.bmp",
  "{X=176,Y=256}, {Width=120, Height=122}": "d:\\Rectangles\\rectangle3.bmp",
  "{X=202,Y=240}, {Width=24, Height=16}": "d:\\Rectangles\\rectangle4.bmp",
  "{X=190,Y=98}, {Width=78, Height=190}": "d:\\Rectangles\\rectangle5.bmp",
  "{X=231,Y=86}, {Width=128, Height=174}": "d:\\Rectangles\\rectangle6.bmp",
  "{X=81,Y=94}, {Width=433, Height=293}": "d:\\Rectangles\\rectangle7.bmp"
}{
  "{X=79,Y=117}, {Width=278, Height=119}": "d:\\Rectangles\\rectangle1.bmp",
  "{X=75,Y=101}, {Width=412, Height=195}": "d:\\Rectangles\\rectangle2.bmp",
  "{X=176,Y=256}, {Width=120, Height=122}": "d:\\Rectangles\\rectangle3.bmp",
  "{X=202,Y=240}, {Width=24, Height=16}": "d:\\Rectangles\\rectangle4.bmp",
  "{X=190,Y=98}, {Width=78, Height=190}": "d:\\Rectangles\\rectangle5.bmp",
  "{X=231,Y=86}, {Width=128, Height=174}": "d:\\Rectangles\\rectangle6.bmp",
  "{X=81,Y=94}, {Width=433, Height=293}": "d:\\Rectangles\\rectangle7.bmp",
  "{X=147,Y=57}, {Width=114, Height=261}": "d:\\Rectangles\\rectangle8.bmp"
}
6rvt4ljy

6rvt4ljy1#

你的代码中有一个bug,每次你把一个json写到一个文件时,你都会把它附加到现有的json中。这会导致你没有有效的json,因为它没有一个根。要修复它,只需打开文件进行覆盖,而不是附加

using (StreamWriter sw = new StreamWriter(@"d:\rectangles.txt", false))
                        {
                            sw.Write(json);
                            sw.Close();
                        }

并且可能必须从空文件开始,因为现有文件无效。

3xiyfsfu

3xiyfsfu2#

约翰最后一个,
您需要创建一个类来存储数据,类似于:

internal class Rect
{
    public Point Location { get; set; }
    public Size Size { get; set; }
    public string FileName { get; set; }

    public Rect()
    {

    }

    public Rect(Point location, Size size, string fileName)
    {
        Location = location;
        Size = size;
        FileName = fileName;
    }
}

创建一个List变量,并使用rectangles.txt文件中的值填充该变量
要将List数据存储到json,请执行以下操作:

List<Rect> myRect; // I'll assume you have this populated with your data.
string jsonData = JsonConvert.SerializeObject(myRect, Formatting.Indented);
//Write the json data to a file
File.WriteAllText("Rectangles.json", jsonData);

“Rectangles.json”的示例内容

[
  {
    "Location": "79, 117",
    "Size": "278, 119",
    "FileName": "d:\\Rectangles\\rectangle1.bmp"
  },
  {
    "Location": "75, 101",
    "Size": "412, 195",
    "FileName": "d:\\Rectangles\\rectangle2.bmp"
  },
  {
    "Location": "176, 256",
    "Size": "120, 122",
    "FileName": "d:\\Rectangles\\rectangle3.bmp"
  },
  {
    "Location": "202, 240",
    "Size": "24, 16",
    "FileName": "d:\\Rectangles\\rectangle4.bmp"
  },
  {
    "Location": "190, 98",
    "Size": "78, 190",
    "FileName": "d:\\Rectangles\\rectangle5.bmp"
  },
  {
    "Location": "231, 86",
    "Size": "128, 174",
    "FileName": "d:\\Rectangles\\rectangle6.bmp"
  },
  {
    "Location": "81, 94",
    "Size": "433, 293",
    "FileName": "d:\\Rectangles\\rectangle7.bmp"
  },
  {
    "Location": "147, 57",
    "Size": "114, 261",
    "FileName": "d:\\Rectangles\\rectangle8.bmp"
  }
]

最后,要将json数据反序列化回一个对象,请将json数据读入一个字符串变量,并将其反序列化,如下所示:

string jsonData = File.ReadAllText("Rectangles.json");
List<Rect> myRects = JsonConvert.DeserializeObject<List<Rect>>(jsonData);

变量“myRects”现在应该是一个List对象,其中包含从jsonData变量反序列化的所有数据。
我希望这能帮上忙。
若翰

相关问题