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


C# JsonObject.ReadAsType方法代码示例

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


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

示例1: TestDataContractJsonSerializerSettings

        public void TestDataContractJsonSerializerSettings()
        {
            TestTypeForSerializerSettings instance = new TestTypeForSerializerSettings
            {
                BaseRef = new DerivedType(),
                Date = AnyInstance.AnyDateTime,
                Dict = new Dictionary<string, object>
                {
                    { "one", 1 },
                    { "two", 2 },
                    { "two point five", 2.5 },
                }
            };

            JsonObject dict = new JsonObject
            {
                { "one", 1 },
                { "two", 2 },
                { "two point five", 2.5 },
            };

            JsonObject equivalentJsonObject = new JsonObject
            {
                { "BaseRef", new JsonObject { { "__type", "DerivedType:NS" } } },
                { "Date", AnyInstance.AnyDateTime },
                { "Dict", dict },
            };

            JsonObject createdFromType = JsonValueExtensions.CreateFrom(instance) as JsonObject;
            Assert.Equal(equivalentJsonObject.ToString(), createdFromType.ToString());

            TestTypeForSerializerSettings newInstance = equivalentJsonObject.ReadAsType<TestTypeForSerializerSettings>();
            // DISABLED, 198487 - Assert.Equal(instance.Date, newInstance.Date);
            Assert.Equal(instance.BaseRef.GetType().FullName, newInstance.BaseRef.GetType().FullName);
            Assert.Equal(3, newInstance.Dict.Count);
            Assert.Equal(1, newInstance.Dict["one"]);
            Assert.Equal(2, newInstance.Dict["two"]);
            Assert.Equal(2.5, Convert.ToDouble(newInstance.Dict["two point five"], CultureInfo.InvariantCulture));
        }
开发者ID:haoduotnt,项目名称:aspnetwebstack,代码行数:39,代码来源:JsonValueExtensionsTest.cs

示例2: ReadAsComplexTypeTest

        public void ReadAsComplexTypeTest()
        {
            JsonObject target = new JsonObject
            {
                { "Name", AnyInstance.AnyString },
                { "Age", AnyInstance.AnyInt },
                { "Address", new JsonObject { { "Street", Address.AnyStreet }, { "City", Address.AnyCity }, { "State", Address.AnyState } } },
            };

            Person person = target.ReadAsType<Person>();
            Assert.Equal(AnyInstance.AnyString, person.Name);
            Assert.Equal(AnyInstance.AnyInt, person.Age);
            Assert.NotNull(person.Address);
            Assert.Equal(Address.AnyStreet, person.Address.Street);
            Assert.Equal(Address.AnyCity, person.Address.City);
            Assert.Equal(Address.AnyState, person.Address.State);
        }
开发者ID:haoduotnt,项目名称:aspnetwebstack,代码行数:17,代码来源:JsonObjectTest.cs


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