본문 바로가기

C#/과제

2020.04.21 File 클래스 json이용 과제

-----------------------------------------------------------------------------------------------------------------------------------

ItemData Class

-----------------------------------------------------------------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Repeat_013
{
     enum eItemType
    { 
        Weapon,
        Armor,
        Accessory,
    }
    enum eGrade
    { 
        Normal,
        Rare,
        Epic,
        Legend,
    }
 
    class ItemData
    {
 
        public int id;
        public eItemType item_type;
        public string name;
        public eGrade grade;
        public int damage;
        public int armor;
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

App Class

-----------------------------------------------------------------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.XPath;
 
namespace Repeat_013
{
    class App
    {
        public App()
        {   
            string path = "./item_data.json";
            string json = File.ReadAllText(path);
            ItemData[] arrItemDatas = JsonConvert.DeserializeObject<ItemData[]>(json);
 
            Dictionary<int, ItemData> dicItemDatas = new Dictionary<int, ItemData>();
 
            foreach (ItemData data in arrItemDatas)
            {
                dicItemDatas.Add(data.id, data);
            }
 
            //foreach (KeyValuePair<int, ItemData> pair in dicItemDatas)
            //{
            //    ItemData data = pair.Value;
            //    Console.WriteLine("{0} {1}", data.id, data.name);
            //}
 
            foreach (KeyValuePair<int, ItemData> pair in dicItemDatas)
            {
                Console.WriteLine("id : {0}\nitem_type : {1}\nname : {2}\ngrade : {3}\ndamage : {4}\narmor :  {5}",
                Console.WriteLine("-------------------------------");
            }
        }
 
    }
 
}
 
 
 

 

-----------------------------------------------------------------------------------------------------------------------------------

출력값

-----------------------------------------------------------------------------------------------------------------------------------

 

-----------------------------------------------------------------------------------------------------------------------------------

※주의할점

1. 바인딩(맵핑)클래스를 만들었을 때, 엑셀의 컬럼명과 맴버 변수 명을 동일하게 해야한다.

2. 메인에 new App()적는거 잊지 말자..(대체왜안되지하면서 15분동안 삽질했는데, 알고보니 메인에 new App()을 안함...)

 

'C# > 과제' 카테고리의 다른 글