using System;
using System.IO; //using 지시문으로 네임스페이스 추가
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class App
{
//생성자
public App()
{
//1. json파일 변수에 넣고 파일의 문자들을 읽어들여서 변수에 넣기
//2. 역직렬화 하기 (배열. JsonConvert.DeserializeObject<타입>(json값)
//3. 배열의 요소들을 Dictionary에 추가하기 (foreach문 사용)
//4. Dictionary에 잘 추가됐는지 출력해서 확인하고 마무리.
string path = "./monster_data.json";
string json = File.ReadAllText(path);
MonsterData[] monsterDatas = JsonConvert.DeserializeObject<MonsterData[]>(json);
Dictionary<int, MonsterData> dicMonsterData = new Dictionary<int, MonsterData>();
foreach (MonsterData data in monsterDatas)
{
dicMonsterData.Add(data.id, data);
}
foreach (KeyValuePair<int,MonsterData> pair in dicMonsterData)
{
Console.WriteLine("key: {0}, val: (hp:{1}) (damage:{2}) (armor:{3})", pair.Key, pair.Value.hp, pair.Value.damage, pair.Value.armor);
}
}
}
}
using System;
namespace HelloWorld
{
public class MonsterData
{
public int id;
public string name;
public float hp;
public float damage;
public float armor;
}
}
'C# > 수업 내용' 카테고리의 다른 글
event (0) | 2021.12.24 |
---|---|
json저장, 불러오기 (0) | 2021.12.24 |
파일 읽기, 역직렬화 연습 (0) | 2021.12.24 |
람다식 연습 15 (Comparision) (0) | 2021.12.24 |
람다식 연습 8 (Action) (0) | 2021.12.24 |