亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

c# litjson如何解析復雜JSON結構

c#
小樊
92
2024-06-24 10:38:35
欄目: 編程語言

在C#中,可以使用LitJSON庫來解析復雜的JSON結構。以下是一個簡單的示例代碼,演示如何使用LitJSON解析一個包含嵌套結構和數組的JSON:

using LitJson;
using System;

class Program
{
    static void Main()
    {
        string json = @"
        {
            ""name"": ""John Smith"",
            ""age"": 30,
            ""isMarried"": true,
            ""children"": [
                {
                    ""name"": ""Alice"",
                    ""age"": 5
                },
                {
                    ""name"": ""Bob"",
                    ""age"": 3
                }
            ],
            ""address"": {
                ""street"": ""123 Main St"",
                ""city"": ""New York"",
                ""zipCode"": ""10001""
            }
        }";

        JsonData data = JsonMapper.ToObject(json);

        string name = (string)data["name"];
        int age = (int)data["age"];
        bool isMarried = (bool)data["isMarried"];

        JsonData children = data["children"];
        foreach (JsonData child in children)
        {
            string childName = (string)child["name"];
            int childAge = (int)child["age"];
            Console.WriteLine("Child: {0} ({1} years old)", childName, childAge);
        }

        JsonData address = data["address"];
        string street = (string)address["street"];
        string city = (string)address["city"];
        string zipCode = (string)address["zipCode"];

        Console.WriteLine("Name: {0}", name);
        Console.WriteLine("Age: {0}", age);
        Console.WriteLine("Married: {0}", isMarried);
        Console.WriteLine("Address: {0}, {1}, {2}", street, city, zipCode);
    }
}

在這個示例中,首先將JSON字符串轉換為JsonData對象,然后通過索引訪問對象的屬性和數組元素,從而獲取JSON中的數據。通過遞歸訪問嵌套結構和數組,可以方便地解析復雜的JSON數據。

0
南汇区| 五原县| 平顺县| 紫金县| 封丘县| 陆丰市| 胶南市| 额敏县| 仪征市| 永新县| 临猗县| 常德市| 营口市| 凌海市| 开化县| 桑植县| 怀来县| 富源县| 沁水县| 辽宁省| 沐川县| 青阳县| 海城市| 昔阳县| 武邑县| 霍州市| 金昌市| 绥滨县| 柞水县| 邵阳市| 伊春市| 库车县| 巴彦县| 收藏| 赣榆县| 贵南县| 云阳县| 凯里市| 奉贤区| 阳新县| 时尚|