首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用json从文本文件反序列化回dictionary<string,string>?

如何使用json从文本文件反序列化回dictionary<string,string>?
EN

Stack Overflow用户
提问于 2022-10-06 00:09:14
回答 2查看 83关注 0票数 0

我正在使用Newtonsoft.Json包。

在表格的顶部:

代码语言:javascript
运行
复制
Dictionary<string, string> FileList = new Dictionary<string, string>();

这是我每次添加到json文本文件时,首先将其序列化为鼠标向上事件中的文本文件的方式:

代码语言:javascript
运行
复制
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();
                        }

然后在构造函数中

代码语言:javascript
运行
复制
FileList = JsonConvert.DeserializeObject<Dictionary<string, string>>(@"d:\rectangles.txt");

但我在这句话上错了:

Newtonsoft.Json.JsonReaderException:“解析值: d. Path”时遇到的意外字符,第0行,位置0。

我尝试过这样做,首先将文件内容读入string变量,然后反序列化string变量:

代码语言:javascript
运行
复制
string g = File.ReadAllText(@"d:\rectangles.txt");
FileList = JsonConvert.DeserializeObject<Dictionary<string, string>>(g);

但是现在出错了:

Newtonsoft.Json.JsonReaderException HResult=0x80131500 Message=Additional文本在读取完JSON内容后遇到:{。路径'',第3行,位置1. Source=Newtonsoft.Json

这是文件内容,不是g变量内容,而是文件:

代码语言:javascript
运行
复制
"{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"
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-10-06 03:05:09

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

代码语言:javascript
运行
复制
using (StreamWriter sw = new StreamWriter(@"d:\rectangles.txt", false))
                        {
                            sw.Write(json);
                            sw.Close();
                        }

也许您必须从空文件开始,因为现有的文件是无效的。

票数 1
EN

Stack Overflow用户

发布于 2022-10-06 02:26:42

最后一位,

您需要创建一个类来存储类似于以下内容的数据:

代码语言:javascript
运行
复制
    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;
    }
}

创建一个列表变量并使用您的rectangles.txt文件中的值填充它

要将列表数据存储到json,请执行以下操作:

代码语言:javascript
运行
复制
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“的示例内容

代码语言:javascript
运行
复制
[
  {
    "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数据读入字符串变量并按如下方式反序列化它:

代码语言:javascript
运行
复制
string jsonData = File.ReadAllText("Rectangles.json");
List<Rect> myRects = JsonConvert.DeserializeObject<List<Rect>>(jsonData);

变量"myRects“现在应该是一个List对象,它包含从jsonData变量反序列化的所有数据。

我希望这能帮到你。

约翰

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73967449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档