Unity/수업내용(C#)

[3.25] C#으로 json파일 불러오기 복습 1

ljw4104 2021. 3. 25. 10:08

 

song_data.xml

using System;

namespace Study11
{
    public class SongData
    {
        public int songId;
        public string songName;
        public string composer;
        public int pastDifficulty;
        public int presentDifficulty;
        public float futureDifficulty;
        public bool haveBeyond;
        public float beyondDifficulty;

        public override string ToString()
        {
            //문자열 보간 사용
            return String.Format("{0 ,-2} {1, -35} {2, -35} {3, 13} {4, 3} {5, 5} {6, 5} {7, 5}",
                this.songId, this.songName, this.composer, this.pastDifficulty, this.presentDifficulty, this.futureDifficulty, this.haveBeyond, this.beyondDifficulty);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Newtonsoft.Json;

namespace Study11
{
    public class App
    {
        public App()
        {
            string path = File.ReadAllText("./song_data.json");
            SongData[] songDatas = JsonConvert.DeserializeObject<SongData[]>(path);

            Dictionary<int, SongData> dicSongDatas = songDatas.ToDictionary(x => x.songId);
            foreach (var song in dicSongDatas)
            {
                Console.WriteLine(song.Value);
            }
        }
    }
}