본문 바로가기
프로그래밍/C#

JSON 점 표기법(Dot Notation)에 대해서

by bantomak 2025. 3. 6.
반응형

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

댓글