0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

System.Text.Jsonのシリアライズで躓いた点について...。[C#]

Last updated at Posted at 2021-06-05

    class JSON
    {
        internal List<string> Word { get; set; }

        internal string Test { get; set; }

        internal string Test2 { get; set; }

        internal int[] Test3 { get; set; }
    }

    class Program
    {
        static void Main()
        {
            JSON testString = new JSON()
            {
                Word = new List<string>
                {
                    "Apple",
                    "Banana",
                    "Pine",
                },
                Test = "Hello",
                Test2 = "HEllo222",
                Test3 = new int[]
                {
                    1,
                    2,
                    3,
                },
            };

            //シリアライズ
            string jsonString = JsonSerializer.Serialize(testString, new JsonSerializerOptions { WriteIndented = true});

            //JSON文字列をテキストファイルに保存
            File.WriteAllText(@"C:\Users\user\test.txt", jsonString);            
        }

上の図のようにデータクラス(JSON.cs)を用意して、Program.csでJSON文字列にしていこう思ったのですが、上手くいきませんでした。
原因はデータクラス(JSON.cs)のプロパティのアクセス修飾子がinternalだったからです。
下の図のようにデータクラス(JSON.cs)のアクセス修飾子をpublicにすれば上手くいきます。


    class JSON
    {
        public List<string> Word { get; set; }

        public string Test { get; set; }

        public string Test2 { get; set; }

        public int[] Test3 { get; set; }
    }

しかし何故internalではいけないのか分かりませんでした。
分かり次第、追記します。(2020/6/5更新)

0
1
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?