当前位置: 首页>>代码示例>>C#>>正文


C# JsonArray.ToObjectArray方法代码示例

本文整理汇总了C#中JsonArray.ToObjectArray方法的典型用法代码示例。如果您正苦于以下问题:C# JsonArray.ToObjectArray方法的具体用法?C# JsonArray.ToObjectArray怎么用?C# JsonArray.ToObjectArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在JsonArray的用法示例。


在下文中一共展示了JsonArray.ToObjectArray方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main


//.........这里部分代码省略.........
            Console.WriteLine("Working with values");

            bool isTrue = ja[0].OrderHistory == null;
            Console.WriteLine("Null comparison {0}", isTrue);

            // TODO Currently we have a bug where if you add a null to a type
            // it will cause ToObjectArray (used later) to throw an exception
            ja[0].Remove("OrderHistory");

            isTrue = ja[0].OrderAmount > 20000;
            Console.WriteLine("Relational operators {0}", isTrue);

            isTrue = ja[0].OrderAmount + 10000 == 40000;
            Console.WriteLine("Arithmetic operators {0}", isTrue);

            isTrue = !ja[0].IsMarried;
            Console.WriteLine("Logical operators {0}" + Environment.NewLine, isTrue);

            Console.WriteLine("Get a value parsed from inside a JSON string");

            Guid id = ja[0].ID;
            Console.WriteLine(id + Environment.NewLine);

            Console.WriteLine("Get DateTime parsed from inside a JSON string");

            // Relative date format - relative to this machine time
            DateTime dob1 = ja[0].DOB;

            // Absolute date format - fixed to reference UTC
            DateTime dob2 = ja[1].DOB;
            DateTime dob3 = ja[2].DOB;
            Console.WriteLine("{0}, {1}, {2}" + Environment.NewLine, dob1, dob2, dob3);

            Console.WriteLine("Get DateTimeOffset parsed from inside a JSON string");

            // Relative date format - relative to this machine time
            DateTimeOffset dobOffset1 = ja[0].DOB;

            // Absolute date format - fixed to reference UTC
            DateTimeOffset dobOffset2 = ja[1].DOB;
            DateTimeOffset dobOffset3 = ja[2].DOB;
            Console.WriteLine("{0}, {1}, {2}" + Environment.NewLine, dobOffset1, dobOffset2, dobOffset3);

            Console.WriteLine("Try get a value parsed from inside a JSON string " +
                "but encounter wrong format in the string");

            try
            {
                int wrongType = ja[0].ID;
                Console.WriteLine(wrongType);
            }
            catch (InvalidCastException)
            {
                Console.WriteLine("Could not parse the required format and threw an exception");
            }

            Console.WriteLine();

            /// If you prefer to not use an exception-based model to handle invalid
            /// cast/parse operations, TryReadAs<T>(out T valueOfT) and ReadAs<T>(T fallback)
            /// are still available to you, however they are not discoverable because
            /// dynamic types don't generate IntelliSense. For an example of using
            /// these methods, check the JsonValueBasic sample.

            Console.WriteLine("Index into the type");

            Guid firstFriend = ja[0].Friends[0];
            Console.WriteLine(firstFriend + Environment.NewLine);

            try
            {
                Guid wrongIndex = ja[0].Friends[10];
                Console.WriteLine(wrongIndex);
            }
            catch (InvalidCastException)
            {
                Console.WriteLine("Tried to index into type but specified invalid index " +
                    "and only got an exception when trying to access the value itself.");
            }

            /// Indexing into a type will never yield an exception along the lines of
            /// KeyNotFoundException or ArgumentOutOfRangeException, because under the
            /// covers this uses the ValueOrDefault method. To learn more about that method
            /// see the JsonValueBasic sample. If the user indexes into an invalid element,
            /// they will only get an exception when they try to access the value itself
            /// This is nice since you don't have to worry about catching exceptions due to
            /// invalid indices - just catch the final exception when the value is accessed.

            Console.WriteLine("Strip out JsonValue hierarchy to get regular CLR types");

            object[] unwrapped = ja.ToObjectArray();
            Console.WriteLine(
                "Type of parent {0} and type of children {1}" + Environment.NewLine,
                unwrapped.GetType(),
                unwrapped[0].GetType());

            /// Similarly you can use ToDictionary() if the class is a JsonObject

            Console.ReadLine();
        }
开发者ID:nuxleus,项目名称:WCFWeb,代码行数:101,代码来源:Program.cs


注:本文中的JsonArray.ToObjectArray方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。