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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace Study_013_1
{
public class App
{
public Dictionary<int, ItemData> itemDataDict;
public App()
{
/*
//Dictionary 예제.
var dict = new Dictionary<int, string>();
dict.Add(100, "장검");
dict.Add(200, "단검");
dict.Add(300, "활");
Console.WriteLine("{0} : {1}", 100, dict[100]);
Console.WriteLine("{0} : {1}", 200, dict[200]);
foreach (var kvp in dict)
Console.WriteLine("키 : {0}, 값 : {1}", kvp.Key, kvp.Value);
foreach (var kvp in dict)
Console.WriteLine("키 : {0}, 값 : {1}", kvp.Key, kvp.Value);
foreach(string value in dict.Values)
{
Console.WriteLine(value);
}
*/
/*
//Dict예제2
var bookDict = new Dictionary<string, string>();
bookDict["0001"] = "어린 왕자";
bookDict["0002"] = "보물섬";
bookDict["0003"] = "동물농장";
//key로 값 출력.
Console.WriteLine(bookDict["0002"]);
//foreach문으로 사전요소 출력.
foreach (var kvp in bookDict)
Console.WriteLine("{0}, {1}", kvp.Key, kvp.Value);
//remove
bookDict.Remove("0003");
//contains
bookDict.ContainsKey("0002");
bookDict.ContainsValue("동물농장");
//count
Console.WriteLine(bookDict.Count);
*/
string json = File.ReadAllText(path);
//문자열 json을 배열객체 (ItemData객체 들어있는)
ItemData[] itemDataArr = JsonConvert.DeserializeObject<ItemData[]>(json);
//사전에 옮겨담음.
itemDataDict = new Dictionary<int, ItemData>();
foreach (ItemData data in itemDataArr)
//해당 ID를 가지고 아이템을 생성한다.
Item item1 = this.CreateItemById(102);
item1.PrintItemInfo();
}
public Item CreateItemById(int id)
{
if (this.itemDataDict.ContainsKey(id))
return new Item(itemDataDict[id]);
return null;
}
}
}
|
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
45
46
47
48
49
50
51
52
53
54
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace Study_013_3
{
public class App
{
public Dictionary<int, ItemData> ItemDataDict;
public App()
{
this.ItemDataDict = new Dictionary<int, ItemData>();
//출력1
string jsonData = File.ReadAllText(fileDir);
//Console.WriteLine(jsonData);
//출력2
string[] jsonLines = File.ReadAllLines(fileDir);
//foreach (string str in jsonLines)
// Console.WriteLine(str);
//출력3
IEnumerator<string> Enumerator = (IEnumerator<string>)File.ReadLines(fileDir);
while (Enumerator.MoveNext())
Console.WriteLine(Enumerator.Current);
ItemData[] itemDataArr = JsonConvert.DeserializeObject<ItemData[]>(jsonData);
foreach (ItemData data in itemDataArr)
Item createdItem = this.CreateItemById(102);
//createdItem.PrintItemInfo();
}
public Item CreateItemById(int id)
{
if (this.ItemDataDict.ContainsKey(id))
return new Item(ItemDataDict[id]);
return null;
}
}
}
|
Study_018-1 아이템 상점 구현. (0) | 2020.04.28 |
---|---|
Study_014 - json변환, 파일입출력, Dictionary (0) | 2020.04.22 |
Study_012 (0) | 2020.04.20 |
Study_010-2 (0) | 2020.04.16 |
Study_010 - 과일, 과일바구니 클래스 배열 (0) | 2020.04.16 |