Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have working method for saving data into JSON file. Now i need to read that file and display in console.

What i'm missing?

Below is my code

My method for reading:

 public static void Read(string path, Workspace workspace)
        {
            using (StreamReader file = new StreamReader(path))
            {
                try
                {
                    string json = file.ReadToEnd();

                    var serializerSettings = new JsonSerializerSettings
                    {
                        ContractResolver = new CamelCasePropertyNamesContractResolver()
                    };

                    var workspace1 = JsonConvert.DeserializeObject<Workspace>(json, serializerSettings);
                    workspace = workspace1;
 
                }
                catch (Exception)
                {
                    Console.WriteLine("Problem reading file");
                }
            }
        }

In program.cs

            Serializer.Save("C:\Users\user\Desktop\test.json", workspace);

            Serializer.Read("C:\Users\user\Desktop\test.json", workspace);


            Console.ReadKey();

On the left is my json file and on the right is what i would like to show in the console.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
261 views
Welcome To Ask or Share your Answers For Others

1 Answer

You are missing the keyword ref:

public static void Read(string path, ref Workspace workspace)

That said, you should not use it. Make the function return a Workspace.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...