Пример #1
0
        public static void RunExample1()
        {
            Console.WriteLine("Example 1");
            Console.WriteLine("=========");
            Console.WriteLine();

            string json = @"
      {
        ""addressBook"": [
          {""lastName"": ""Average"", ""firstName"": ""Joe""},
          {""lastName"": ""Doe"", ""firstName"": ""Jane""},
          {""lastName"": ""Smith"", ""firstName"": ""John""}
        ]
      }";

            var      root = JsonNode.Parse(json);
            JsonNode book = root["addressBook"];

            if (book.IsArray)
            {
                int c = book.Count;
                for (int i = 0; i < c; i++)
                {
                    JsonNode entry = book[i];
                    string   ln    = entry["lastName"];
                    string   fn    = entry["firstName"];
                    Console.WriteLine(fn + " " + ln);
                }
            }

            Console.WriteLine();
        }
Пример #2
0
        public static void RunExample2()
        {
            Console.WriteLine("Example 2");
            Console.WriteLine("=========");
            Console.WriteLine();

            string json = RetrieveJsonExample();

            PrintNode(JsonNode.Parse(json), "demo = ", 0);
            Console.WriteLine();
        }
Пример #3
0
        public static void RunExample4()
        {
            Console.WriteLine("Example 4");
            Console.WriteLine("=========");
            Console.WriteLine();

            string s = @"
      {
        ""options"": { ""logging"": true },

        ""sensors"": [
          { ""name"": ""Button"", ""value"": 1 },
          { ""name"": ""Temperature"", ""value"": 17.5 }
        ],

        ""actors"": [
          { ""name"": ""Lamp"", ""value"": 0 }
        ]
      }";

            /*
             * Console.WriteLine(s.Trim());
             * Console.WriteLine();
             * //*/

            var n = JsonNode.Parse(s);

            Console.WriteLine("n.AsJsonCompact          => " + n.AsJsonCompact);
            Console.WriteLine();

            Console.WriteLine("n[\"options\"].NodeType    => " + n["options"].NodeType);
            Console.WriteLine("n[\"options\"][\"logging\"]  => " + n["options"]["logging"]);
            Console.WriteLine("n[\"sensors\"].NodeType    => " + n["sensors"].NodeType);
            Console.WriteLine("n[\"sensors\"].Count       => " + n["sensors"].Count);
            Console.WriteLine("n[\"sensors\"][0][\"name\"]  => " + n["sensors"][0]["name"]);
            Console.WriteLine("n[\"sensors\"][1][\"value\"] => " + n["sensors"][1]["value"]);
            Console.WriteLine("n[\"missing\"][33][\"foo\"]  => " + n["missing"][33]["foo"]);
            Console.WriteLine();
        }