我正在使用 Newtonsoft.Json 包。
在表格頂部:
Dictionary<string, string> FileList = new Dictionary<string, string>();
這就是我每次添加到 json 文本檔案時首先將其序列化到滑鼠向上事件中的文本檔案的方式:
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);
但現在出現錯誤:
Newtonsoft.Json.JsonReaderException HResult=0x80131500 Message=完成讀取 JSON 內容后遇到的附加文本:{. 路徑'',第 3 行,位置 1。Source=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"
}
uj5u.com熱心網友回復:
您的代碼中有一個錯誤,每次將 json 寫入檔案時,都會將其附加到現有的 json 中。這會導致您沒有有效的 json,因為它沒有根。要修復它只需打開檔案進行覆寫,而不是附加
using (StreamWriter sw = new StreamWriter(@"d:\rectangles.txt", false))
{
sw.Write(json);
sw.Close();
}
也許您將不得不從空檔案開始,因為現有檔案無效。
uj5u.com熱心網友回復:
約翰最后,
您需要創建一個類來存盤您的資料,如下所示:
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 檔案中的值填充它
要將串列資料存盤到 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 變數反序列化的所有資料。
我希望這有幫助。
約翰
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/516767.html
標籤:C#json表格
