Home > Software design >  How to use json to deserialize from text file back to dictionary<string,string>?
How to use json to deserialize from text file back to dictionary<string,string>?

Time:10-06

I'm using the Newtonsoft.Json package.

At the top of the form :

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

this is how i serialize it first to a text file in the mouse up event each time i'm adding to the json text file :

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();
                        }

then in the constructor

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

but i'm getting error on that line :

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: d. Path '', line 0, position 0.'

I tried this, first to read the file content to string variable then to deserialize the string variable :

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

but getting now error :

Newtonsoft.Json.JsonReaderException HResult=0x80131500 Message=Additional text encountered after finished reading JSON content: {. Path '', line 3, position 1. Source=Newtonsoft.Json

This is the file content not the g variable content but the file :

"{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"
}

CodePudding user response:

you have a bug in your code , each time when you write a json to a file, you append it to existing json. This causes that you have not valid json, since it doesn't have a root. To fix it just open file for overwriting, not appending

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

and maybe you will have to start from the empty file since the existing one is not valid.

CodePudding user response:

jhon last,

You'll want to create a class to store your data similar to this:

    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;
    }
}

Create a List variable and populate it with your values from your rectangles.txt file

To store the List data to json, do something like this:

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);

Example contents of "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"
  }
]

Finally, to deserialize your json data back to an object, then read the json data into a string variable and deserialize it like this:

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

The variable "myRects" should now be a List object that contains all of the data deserialized from the jsonData variable.

I hope this helps.
John

  • Related