1은 json으로 찾아봐. 암튼 json2임
▶️ 목차
1. Newtonsoft.Json 파일 설치: 역직렬화를 해주는 프로그램
2. json 파일 읽기
3.역직렬화 할 대상 맵핑 클래스 생성
4. Dictionary 컬렉션 인스턴스화 new 하기
5.DeserializeObject 로 역직렬(문자열>객체)화 하기
* 딕셔너리 요소 값 가져오기
*딕셔너리 요소 순회
* 예시 코드: Program / WeaponData / App / 출력
1. Newtonsoft.Json 파일 설치: 역직렬화를 해주는 프로그램
- 직렬화: 객체 > 문자열
- 역직렬화: 문자열 > 객체
>> 설치되있으면 2. 로 <<
vv > 도구 > NuGet 패키지 관리자 > 솔루션용 NuGet 패키지 관리.. >
찾아보기(Tab) > Newtonsoft.Json 선택 >
우측창) 설치 상태 확인: 설치되지 않음 이면 설치해라.
- 설치 확인
2. json 파일 읽기
> 헤더에 using System.IO; 추가
using System.IO;
> 저장 위치에서 json 파일 불러오기
string json = File.ReadAllText("./weapon_data.json"); |
- 저장 위치: json1에 보면 솔루션 실행 파일이 있는 Debug 폴더에 저장했어. 기억 안나면 1 보고 와.
- 변환 타입: string >> 역직렬화하는 거잖아. 문자열로 저장되있으니 객체로 바꾸는 거야.
잘적용했다. 고생했어👍 이제 써보자~
3.역직렬화 할 대상 맵핑 클래스 생성
맵핑 클래스 이름은 엑셀 데이터 파일 이름과 동일하게 만들면 알아보기 좋겠지?
- ! 맵핑 클래스는 생성자를 만들지 않음
- 데이터 테이블에 적은 이름 그대로(소문자 대문자 등) 변수 생성(유효화 검사 때 지웠었다.)
4. Dictionary 컬렉션 인스턴스화 new 하기
Dictionary<int, WeaponData> dic = new Dictionary<int, WeaponData>();
5.DeserializeObject 로 역직렬(문자열>객체)화 하기
WeaponData[] weaponDatas =
JsonConvert.DeserializeObject<WeaponData[]>(json);
- Dictionary로 컬렉션 배열로 만들었으니까 배열 타입[ ]
길지만 당황하지 말고 들어 = 오른쪽은 값이야.
> 이쯤에서 잘불러서 weaponDatas 에 잘 들어갔는지 확인해 보자.
Console.WriteLine("▶▶▶weaponData 확인");
for (int i = 0; i < weaponDatas.Length; i++)
{
WeaponData weaponData = weaponDatas[i];
Console.WriteLine($"{weaponData.id}. {weaponData.name}");
dic.Add(weaponData.id, weaponData);
}
- 딕셔너리 요소 값 가져오기
// 딕셔너리 100번 키-값 가져오기
Console.WriteLine("▶▶▶WeaponData 클래스를 참조하여 딕셔너리 id.100 요소 값 가져오기");
WeaponData data = dic[100];
Console.WriteLine($"{data.id} {data.name}");
- dic[100] 만 호출해도 딕셔너리는 키-벨류가 한 쌍이라 둘 다 옴
- 딕셔너리 요소 순회
- 아무튼, 무조건, 어찌됐든 반박 불가. 반드시 딕셔너리는 순회할 때 foreach(읽기 전용)
vhdlcl dskTmaus Qkreorkfl.
//딕셔너리 요소 순회
//반드시 포이치 사용
Console.WriteLine("▶▶▶딕셔너리 요소 순회\n♪무조건 무조건이야~♪ foreach(읽기전용)로 불러와야 해♪");
foreach (KeyValuePair<int, WeaponData> pair in dic)
{
WeaponData elementData = pair.Value;
Console.WriteLine($"{pair.Key}) {elementData.name}");
}
- Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnCollection
{
internal class Program
{
static void Main(string[] args)
{
new App();
}
}
}
- WeaponData
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnCollection
{
internal class WeaponData
{
//생성자 ㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴ
//속성 순서대로 소문자, 대문자 맞춰서!
public int id;
public string name;
}
}
- App
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnCollection
{
public class App
{
//생성자
public App()
{
Console.WriteLine("App생성자 호출됨");
Console.WriteLine("▶▶▶json 파일 읽기");
// 1.json 파일 읽기
//using System.IO;
string json = File.ReadAllText("./weapon_data.json");
Console.WriteLine(json);
Console.WriteLine();
Console.WriteLine();
//딕셔너리 컬렉션 인스턴스화
Dictionary<int, WeaponData> dic = new Dictionary<int, WeaponData>();
Console.WriteLine("▶▶▶딕셔너리 컬렉션 인스턴스화 했음");
Console.WriteLine();
Console.WriteLine();
//2. 역직렬화 (문자열>객체)
//역직렬화 대상 클래스 생성 : 매핑클래스!
//클래스==json데이터
WeaponData[] weaponDatas =
JsonConvert.DeserializeObject<WeaponData[]>(json);
Console.WriteLine("▶▶▶DeserializeObject 역직렬(문자열>객체)화 했음");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("▶▶▶weaponData 확인");
for (int i = 0; i < weaponDatas.Length; i++)
{
WeaponData weaponData = weaponDatas[i];
Console.WriteLine($"{weaponData.id}. {weaponData.name}");
dic.Add(weaponData.id, weaponData);
}
Console.WriteLine();
Console.WriteLine();
// 딕셔너리 요소 값 가져오기
Console.WriteLine("▶▶▶WeaponData 클래스를 참조하여 딕셔너리 id.100 요소 값 가져오기");
WeaponData data = dic[100];
Console.WriteLine($"{data.id} {data.name}");
Console.WriteLine();
Console.WriteLine();
//딕셔너리 요소 순회
//반드시 포이치 사용
Console.WriteLine("▶▶▶딕셔너리 요소 순회\n♪무조건 무조건이야~♪ foreach(읽기전용)로 불러와야 해♪");
foreach (KeyValuePair<int, WeaponData> pair in dic)
{
WeaponData elementData = pair.Value;
Console.WriteLine($"{pair.Key}) {elementData.name}");
}
Console.WriteLine();
Console.WriteLine();
}
}
}
- 출력
끝. 여기까지 외우지 말고 다음에 또 와~
'정리' 카테고리의 다른 글
에러 확인/ 속도 변경(정지) / 보는 방향 변경 (0) | 2024.09.20 |
---|---|
Text(legacy)가 없습니다. (1) | 2024.08.20 |
엑셀에 입력한 data를 ★json★ 파일로 변환하는 법1 (2) | 2024.08.14 |
리스트 작성법 (0) | 2024.08.13 |
배열 작성법 (0) | 2024.08.13 |