반응형
JSON 점 표기법(JSON Dot Notation)
JSON 형식으로 커뮤니케이션을 진행할 때 JSON 점 표기법을 이용하는 경우가 많다. 점 표기법은 JSON 구조에서 특정 필드(속성)에 접근할 때 parent.child 형식으로 중첩된 JSON 객체의 속성을 간결하게 표현하는 방법이다.
해당 방식으로 JSON의 값들을 표현하고, 특정 값을 쉽고 빠르게 전달할 수 있도록 도와준다.
기본 JSON 예제
{
"user": {
"id": 123,
"name": "Alice",
"address": {
"city": "New York",
"zip": "10001"
}
},
"action": "Login"
}
- user.id : 123
- user.name : "Alice"
- user.address.city : "New York"
- user.address.zip : "10001"
- action : "Login"
C#에서 JSON Dot Notation 사용 방법
C#에서 System.Text.Json 라이브러리를 사용하여 JSON에서 점 표기법을 활용할 수 있다.
using System;
using System.Text.Json;
class Program
{
static void Main()
{
string jsonString = @"{
""user"": {
""id"": 123,
""name"": ""Alice"",
""address"": {
""city"": ""New York"",
""zip"": ""10001""
}
},
""action"": ""Login""
}";
using JsonDocument doc = JsonDocument.Parse(jsonString);
JsonElement root = doc.RootElement;
// ✅ 점 표기법을 이용한 JSON 데이터 접근
int userId = root.GetProperty("user").GetProperty("id").GetInt32();
string userName = root.GetProperty("user").GetProperty("name").GetString();
string userCity = root.GetProperty("user").GetProperty("address").GetProperty("city").GetString();
string action = root.GetProperty("action").GetString();
Console.WriteLine($"User ID: {userId}");
Console.WriteLine($"User Name: {userName}");
Console.WriteLine($"City: {userCity}");
Console.WriteLine($"Action: {action}");
}
}
✅ JSON Dot Notaion의 장점
- 간결하고 직관적인 데이터 접근
- 중첩된 JSON 객체에서도 점 표기법을 사용하면 특정 속성을 빠르게 찾을 수 있음
- 배열 인덱싱 지원
- user[0].name과 같은 점 표기법을 사용하면 JSON 배열의 특정 요소에도 접근 가능
JSON 배열과 Dot Notation
Dot Notation을 통해서 Json 배열에도 쉽게 접근이 가능하다.
{
"user": {
"id": 123,
"name": "Alice",
"address": {
"city": "New York",
"zip": "10001"
},
"users": [
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
]
},
"action": "Login"
}
- user.users[0].id : 1
- user.users[0].name : "Alice"
- user.users[1].id : 2
- user.users[1].name : "Bob"
using System;
using System.Text.Json;
class Program
{
static void Main()
{
string jsonString = @"{
""user"": {
""id"": 123,
""name"": ""Alice"",
""address"": {
""city"": ""New York"",
""zip"": ""10001""
},
""users"": [
{ ""id"": 1, ""name"": ""Alice"" },
{ ""id"": 2, ""name"": ""Bob"" }
]
},
""action"": ""Login""
}";
using JsonDocument doc = JsonDocument.Parse(jsonString);
JsonElement root = doc.RootElement;
int users_id = root.GetProperty("user").GetProperty("users")[0].GetProperty("id").GetInt32();
string users_name = root.GetProperty("user").GetProperty("users")[0].GetProperty("name").GetString();
Console.WriteLine($"id: {users_id}");
Console.WriteLine($"name: {users_name}");
}
}
정리하자면
- JSON Dot Notation(점 표기법)은 중첩된 JSON 데이터에 접근하는 간결한 방법
- C#에서는 System.Text.Json의 GetProperty()를 통해서 점 표기법을 활용 가능하다.
- Json 배열도 [index] 표기법을 사용하여 쉽게 접근 가능
함께 읽으면 좋은 글
C# 간단하게 Json 형식 파싱하기
간단하게 C#으로 Json 형식 파싱하기웹 관련으로 일을 하다보면 Json을 많이 접하겠지만 웹과 거리가 있다보면 Json을 다루는 일은 많지 않다고 생각된다. 간단하게 C#의 System.Text.Json을 이용해서 Json
jettstream.tistory.com
'프로그래밍 > C#' 카테고리의 다른 글
EF Core에서 Find() vs Local.Where() 차이점 (0) | 2025.03.05 |
---|---|
C# 컴퓨터가 문자를 표현하는 방법, 문자 인코딩 (0) | 2025.02.20 |
C# 폴더 안에 있는 모든 파일 찾기 (0) | 2025.01.23 |
C# StackExchange 사용해서 redis에 객체 저장하고 불러오기 (0) | 2025.01.13 |
C# 웹앱에서 appsettings.json 설정값 사용하기 (0) | 2025.01.07 |
댓글