๐ฆ Newtonsoft.Json
๐ง Document
์ฐธ๊ณ ์์ : ๋ฒ ๋ฅด์ ๊ฒ์ ๊ฐ๋ฐ ์ ํ๋ธ
๐ง Settings
https://www.newtonsoft.com/json
- ๋งํฌ ์ ์ ํ Download ๋ฒํผ ํด๋ฆญ
- Json.NET ๋ฒํผ ํด๋ฆญ
- ์ต์ ๋ฒ์ ์ zipํ์ผ ๋ค์ด๋ก๋ ํ ์์ถ ํด์
- Bin\net45์ ์๋ dll ํ์ผ์ ์ ๋ํฐ๋ก ๋๋๊ทธ ์ค ๋๋กญ
- Project Settings / Player / Other Settings / Api Comatibility Level : .NET 4.X
๐ง Object to Json
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
public class JsonTest : MonoBehaviour
{
public JsonTestClass jsonTest_1;
void Start()
{
string jsonData = JsonConvert.SerializeObject(jsonTest_1);
Debug.Log(jsonData);
}
[System.Serializable]
public class JsonTestClass
{
public int i;
public float f;
public bool b;
public string str;
public int[] array;
public List<int> list = new List<int>();
public Dictionary<string, float> dictionary = new Dictionary<string, float>();
public Vector2Int vector;
// ์์ฑ์
public JsonTestClass()
{
i = 1;
f = 2.2f;
b = true;
str = "JSON Test String";
array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
for (int i = 0; i < 5; i++)
{
list.Add(i * 2);
}
dictionary.Add("PI", Mathf.PI);
dictionary.Add("Epsilon", Mathf.Epsilon);
dictionary.Add("Sqrt(2)", Mathf.Sqrt(2));
vector = new Vector2Int(4, 5);
}
public void Print()
{
Debug.Log($"i = {i}");
Debug.Log($"f = {f}");
Debug.Log($"b = {b}");
Debug.Log($"str = {str}");
for (int i = 0; i < array.Length; i++)
{
Debug.Log($"array[{i}] = {array[i]}");
}
for (int i = 0; i < list.Count; i++)
{
Debug.Log($"list[{i}] = {list[i]}");
}
foreach (var item in dictionary)
{
Debug.Log($"dictionary : key = {item.Key} / value = {item.Value}");
}
Debug.Log($"vector = {vector}");
}
}
}
- ์คํฌ๋ฆฝํธ ์์ฑ ํ ์ฝ๋ ์ ๋ ฅ
- ๋น ๊ฒ์ ์ค๋ธ์ ํธ์ ์คํฌ๋ฆฝํธ ์ถ๊ฐ
- ํ๋ ์ดํ๋ฉด ๋ฌธ์์ด๋ก ๋ฐ๋ ๊ฒ์ ํ์ธํ ์ ์์
๐ง Object to Json
public JsonTestClass jsonTest_1;
void Start()
{
string jsonData = JsonConvert.SerializeObject(jsonTest_1);
Debug.Log(jsonData);
JsonTestClass jtest2 = JsonConvert.DeserializeObject<JsonTestClass>(jsonData);
jtest2.Print();
}
- ๋ฌธ์์ด๋ก ๋ฐ๋ ๋ฐ์ดํฐ๋ฅผ jsonํ์์ผ๋ก ๋ณํํ๋ค.
๐ฆ Unity JSON Utility
UnityEngine ๋ค์์คํ์ด์ค์ ํฌํจ๋์ด์๊ธฐ ๋๋ฌธ์ Newtonsoft์ ๋ฌ๋ฆฌ ์ค์นํ๋ ๊ณผ์ ์ด ์๋ค.
๐ง Document
https://docs.unity3d.com/ScriptReference/JsonUtility.html
๐ง ์ฌ์ฉํด๋ณด๊ธฐ
void Start()
{
string jsonData = JsonUtility.ToJson(jsonTest_1);
Debug.Log(jsonData);
JsonTestClass jtest2 = JsonUtility.FromJson<JsonTestClass>(jsonData);
jtest2.Print();
}
-
๐ฆ ์ ๋ฆฌ ๋ฐ ๋น๊ต
๐ง Newtonsoft.Json
- Newtonsoft.Json ๋ค์์คํ์ด์ค ์ถ๊ฐ
- Object to Json : JsonConvert.SerializeObject(๋ฐ์ดํฐ);
- Json to Object : JsonConvert.DeserializeObject<ํด๋์คํ์>(๋ฐ์ดํฐ);
๐ง Unity JSON Utility
- ๋ค์์คํ์ด์ค ์ถ๊ฐํ ํ์ ์์ (UnityEngine)
- Object to Json : JsonUtility.ToJson(๋ฐ์ดํฐ);
- Json to Object : JsonUtility.FromJson<ํด๋์คํ์>(๋ฐ์ดํฐ);
๐ง ์ด์ค๋ฐฐ์ด
- Unity JSON Utility๋ ์ด์ค๋ฐฐ์ด ๋ณํ์ ์ง์ํ์ง ์์๊ณ , Newtonsoft.Jsons๋ ์ง์ํ๋ค.
{
"scores": [
[
0,
1
],
[
2,
3,
4
]
]
}
- TempJson.json ํ์ผ์ ์์ฑํ๋ค.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JsonTest : MonoBehaviour
{
void Start()
{
string temp_json = Resources.Load<TextAsset>("TempJson").text;
TempClass temp_Jutilty = JsonUtility.FromJson<TempClass>(temp_json);
temp_Jutilty.Print();
}
[System.Serializable]
public class TempClass
{
public int[][] scores;
public void Print()
{
foreach (var score in scores)
{
Debug.Log("----- score -----");
foreach (var i in score)
{
Debug.Log($"{i}");
}
}
}
}
}
- JsonUtility๋ฅผ ์ฌ์ฉํด์ ๋ณํํด๋ณด๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
- ๋ณํ์ด ์๋์๊ธฐ ๋๋ฌธ์ scores ๋ณ์์์ Null์ด ๋จ๋ ๊ฒ์ผ๋ก ์ถ์ ๋๋ค.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
public class JsonTest : MonoBehaviour
{
void Start()
{
string temp_json = Resources.Load<TextAsset>("TempJson").text;
TempClass temp_Newtonsoft = JsonConvert.DeserializeObject<TempClass>(temp_json);
temp_Newtonsoft.Print();
}
[System.Serializable]
public class TempClass
{
public int[][] scores;
public void Print()
{
foreach (var score in scores)
{
Debug.Log("----- score -----");
foreach (var i in score)
{
Debug.Log($"{i}");
}
}
}
}
}
- ๋๊ฐ์ด Newtonsoft.Json์ ์ฌ์ฉํด๋ณด๋ฉด ๊ฐ์ด ์ ๋์จ๋ค.
'Unity > Database' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
04. Azure CosmosDB - Delete (0) | 2021.09.06 |
---|---|
03. Azure CosmosDB - Read (0) | 2021.08.29 |
02. Azure CosmosDB - Insert (0) | 2021.08.28 |
01. Azure CosmosDB Settings (0) | 2021.08.21 |
00. Azure ์ฒดํ๊ณ์ ํ์ฑํ (0) | 2021.08.20 |