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


C# JsonObject.Remove方法代码示例

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


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

示例1: ValidJsonObjectRoundTrip

        public void ValidJsonObjectRoundTrip()
        {
            bool oldValue = CreatorSettings.CreateDateTimeWithSubMilliseconds;
            CreatorSettings.CreateDateTimeWithSubMilliseconds = false;
            try
            {
                int seed = 1;
                Log.Info("Seed: {0}", seed);
                Random rndGen = new Random(seed);

                JsonObject sourceJson = new JsonObject(new Dictionary<string, JsonValue>()
                {
                    { "Name", PrimitiveCreator.CreateInstanceOfString(rndGen) },
                    { "Age", PrimitiveCreator.CreateInstanceOfInt32(rndGen) },
                    { "DateTimeOffset", PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen) },
                    { "Birthday", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) }
                });
                sourceJson.Add("NewItem1", PrimitiveCreator.CreateInstanceOfString(rndGen));
                sourceJson.Add(new KeyValuePair<string, JsonValue>("NewItem2", PrimitiveCreator.CreateInstanceOfString(rndGen)));

                JsonObject newJson = (JsonObject)JsonValue.Parse(sourceJson.ToString());

                newJson.Remove("NewItem1");
                sourceJson.Remove("NewItem1");

                Assert.False(newJson.ContainsKey("NewItem1"));

                Assert.False(!JsonValueVerifier.Compare(sourceJson, newJson));
            }
            finally
            {
                CreatorSettings.CreateDateTimeWithSubMilliseconds = oldValue;
            }
        }
开发者ID:haoduotnt,项目名称:aspnetwebstack,代码行数:34,代码来源:JsonStringRoundTripTests.cs

示例2: ChangingEventsTest

        public void ChangingEventsTest()
        {
            const string key1 = "first";
            const string key2 = "second";
            const string key3 = "third";
            const string key4 = "fourth";
            const string key5 = "fifth";
            JsonObject jo = new JsonObject
            {
                { key1, AnyInstance.AnyString },
                { key2, AnyInstance.AnyBool },
                { key3, null },
            };

            TestEvents(
                jo,
                obj => obj.Add(key4, 1),
                new List<Tuple<bool, JsonValue, JsonValueChangeEventArgs>>
                {
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(1, JsonValueChange.Add, key4)),
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(1, JsonValueChange.Add, key4)),
                });

            TestEvents(
                jo,
                obj => obj[key2] = 2,
                new List<Tuple<bool, JsonValue, JsonValueChangeEventArgs>>
                {
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(2, JsonValueChange.Replace, key2)),
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(AnyInstance.AnyBool, JsonValueChange.Replace, key2)),
                });

            TestEvents(
                jo,
                obj => obj[key5] = 3,
                new List<Tuple<bool, JsonValue, JsonValueChangeEventArgs>>
                {
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(3, JsonValueChange.Add, key5)),
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(3, JsonValueChange.Add, key5)),
                });

            jo.Remove(key4);
            jo.Remove(key5);

            TestEvents(
                jo,
                obj => obj.AddRange(new JsonObject { { key4, AnyInstance.AnyString }, { key5, AnyInstance.AnyDouble } }),
                new List<Tuple<bool, JsonValue, JsonValueChangeEventArgs>>
                {
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(AnyInstance.AnyString, JsonValueChange.Add, key4)),
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(AnyInstance.AnyDouble, JsonValueChange.Add, key5)),
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(AnyInstance.AnyString, JsonValueChange.Add, key4)),
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(AnyInstance.AnyDouble, JsonValueChange.Add, key5)),
                });

            TestEvents(
                jo,
                obj => obj.Remove(key5),
                new List<Tuple<bool, JsonValue, JsonValueChangeEventArgs>>
                {
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(AnyInstance.AnyDouble, JsonValueChange.Remove, key5)),
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(AnyInstance.AnyDouble, JsonValueChange.Remove, key5)),
                });

            TestEvents(
                jo,
                obj => obj.Remove("not there"),
                new List<Tuple<bool, JsonValue, JsonValueChangeEventArgs>>());

            jo = new JsonObject { { key1, 1 }, { key2, 2 }, { key3, 3 } };

            TestEvents(
                jo,
                obj => obj.Clear(),
                new List<Tuple<bool, JsonValue, JsonValueChangeEventArgs>>
                {
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, null)),
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, null)),
                });

            jo = new JsonObject { { key1, 1 }, { key2, 2 }, { key3, 3 } };
            TestEvents(
                jo,
                obj => ((IDictionary<string, JsonValue>)obj).Remove(new KeyValuePair<string, JsonValue>(key2, jo[key2])),
                new List<Tuple<bool, JsonValue, JsonValueChangeEventArgs>>
                {
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(2, JsonValueChange.Remove, key2)),
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(2, JsonValueChange.Remove, key2)),
                });

            TestEvents(
                jo,
                obj => ((IDictionary<string, JsonValue>)obj).Remove(new KeyValuePair<string, JsonValue>("key not in object", jo[key1])),
                new List<Tuple<bool, JsonValue, JsonValueChangeEventArgs>>
                {
                });

            TestEvents(
                jo,
                obj => ((IDictionary<string, JsonValue>)obj).Remove(new KeyValuePair<string, JsonValue>(key1, "different object")),
//.........这里部分代码省略.........
开发者ID:haoduotnt,项目名称:aspnetwebstack,代码行数:101,代码来源:JsonObjectTest.cs

示例3: CountTest

        public void CountTest()
        {
            string key1 = AnyInstance.AnyString;
            string key2 = AnyInstance.AnyString2;
            JsonValue value1 = AnyInstance.AnyJsonValue1;
            JsonValue value2 = AnyInstance.AnyJsonValue2;

            JsonObject target = new JsonObject();
            Assert.Equal(0, target.Count);
            target.Add(key1, value1);
            Assert.Equal(1, target.Count);
            target.Add(key2, value2);
            Assert.Equal(2, target.Count);
            target.Remove(key2);
            Assert.Equal(1, target.Count);
        }
开发者ID:haoduotnt,项目名称:aspnetwebstack,代码行数:16,代码来源:JsonObjectTest.cs

示例4: RemoveTest

        public void RemoveTest()
        {
            string key1 = AnyInstance.AnyString;
            string key2 = AnyInstance.AnyString2;
            JsonValue value1 = AnyInstance.AnyJsonValue1;
            JsonValue value2 = AnyInstance.AnyJsonValue2;

            JsonObject target = new JsonObject { { key1, value1 }, { key2, value2 } };

            Assert.True(target.ContainsKey(key1));
            Assert.True(target.ContainsKey(key2));
            Assert.Equal(2, target.Count);

            Assert.True(target.Remove(key2));
            Assert.True(target.ContainsKey(key1));
            Assert.False(target.ContainsKey(key2));
            Assert.Equal(1, target.Count);

            Assert.False(target.Remove(key2));
            Assert.True(target.ContainsKey(key1));
            Assert.False(target.ContainsKey(key2));
            Assert.Equal(1, target.Count);
        }
开发者ID:haoduotnt,项目名称:aspnetwebstack,代码行数:23,代码来源:JsonObjectTest.cs

示例5: FromJson

        /// <summary>
        /// 由Json串给对象赋值,将递归进行调用,碰到JsonArray自动把JsonArray转换成ObjectList。
        /// 碰到JsonObject,自动转换成GeneralOject。
        /// </summary>
        /// <param name="item">从这个json对象给对象属性赋值</param>
        public void FromJson(JsonObject item)
        {
            //通过获取类型,给_ctype赋值
            this.NewGetType();

            //如果有实体类型,则设置实体类型
            if (item.ContainsKey("EntityType"))
            {
                EntityType = item["EntityType"];
                item.Remove("EntityType");
            }
            foreach (string key in item.Keys)
            {
                object value = item[key];
                Log.Debug("from json name=" + this.Name);
                if (key.Equals("id"))
                {
                    Log.Debug("from json id=" + value);
                }
                //如果是数组,对数组中的每一个对象调用转换过程
                if (value is JsonArray)
                {
                    //数组转换成对象列表
                    ObjectList ol = new ObjectList();
                    ol.FromJson(value as JsonArray);
                    SetCollectionProperty(key, ol);
                }
                else if (value is JsonObject)
                {
                    //JsonObject转换成一般对象
                    GeneralObject go = new GeneralObject();
                    go.FromJson(value as JsonObject);
                    SetPropertyValue(key, go, true);
                }
                else if (value is JsonPrimitive)
                {
                    this.NewGetType().GetProperty(key).SetValue(this, value as JsonPrimitive, null);
                }
                else if (value == null)
                {
                    SetPropertyValue(key, null, true);
                }
                else 
                {
                    throw new Exception("类型错误");
                }
            }
            //新加载的对象为未修改
            IsModified = false;
        }
开发者ID:DuBin1988,项目名称:restv2,代码行数:55,代码来源:GeneralObject.cs


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