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


C# JsonValue.Add方法代码示例

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


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

示例1: ReadArray

        static JsonValue ReadArray(string json, ref int i)
        {
            i++; // Skip the '['
            SkipWhitespace(json, ref i);

            JsonValue arrayval = new JsonValue();
            arrayval.Type = JsonType.Array;

            bool expectingValue = false;
            while (json[i] != ']') {
                expectingValue = false;
                arrayval.Add(ReadValue(json, ref i));
                SkipWhitespace(json, ref i);
                if (json[i] == ',') {
                    expectingValue = true;
                    i++;
                    SkipWhitespace(json, ref i);
                } else if (json[i] != ']') {
                    throw new InvalidJsonException("Expected end array token at column " + i + "!");
                }
            }

            if (expectingValue) {
                throw new InvalidJsonException("Unexpected end array token at column " + i + "!");
            }

            i++; // Skip the ']'
            return arrayval;
        }
开发者ID:grahamboree,项目名称:voorhees,代码行数:29,代码来源:JsonReader.cs

示例2: ArrayOperations

    public void ArrayOperations()
    {
        JsonValue test = new JsonValue() { 1, 2, 3 };

        Assert.That(test.Count, Is.EqualTo(3));
        Assert.That(test.Contains(1), Is.True);
        Assert.That(test.Contains(4), Is.False);

        JsonValue[] values = new JsonValue[3];
        test.CopyTo(values, 0);
        for (int i = 0; i < 3; i++) {
            Assert.That((int)values[i], Is.EqualTo((int)test[i]));
        }

        test.Remove(2);
        Assert.That(test.Contains(1), Is.True);
        Assert.That(test.Contains(2), Is.False);
        Assert.That(test.Contains(3), Is.True);

        Assert.That(test.IsReadOnly, Is.False);

        Assert.That(test.IndexOf(1), Is.EqualTo(0));

        test.Insert(1, 2);
        Assert.That((int)test[1], Is.EqualTo(2));
        Assert.That(test.Count, Is.EqualTo(3));

        test.RemoveAt(1);
        Assert.That((int)test[1], Is.EqualTo(3));
        Assert.That(test.Count, Is.EqualTo(2));

        test.Clear();
        Assert.That(test.Count, Is.EqualTo(0));

        test.Add(5);
        Assert.That(test.Count, Is.EqualTo(1));
        Assert.That((int)test[0], Is.EqualTo(5));
    }
开发者ID:grahamboree,项目名称:voorhees,代码行数:38,代码来源:JsonValueTest.cs

示例3: ReadObject

        static JsonValue ReadObject(string json, ref int i)
        {
            var obj = new JsonValue();
            obj.Type = JsonType.Object;
            i++; // Skip the '{'

            SkipWhitespace(json, ref i);
            if (json[i] != '}') {
                //ReadElements(obj, json, ref i);
                while (true) {
                    SkipWhitespace(json, ref i);

                    var key = ReadString(json, ref i);
                    SkipWhitespace(json, ref i);
                    if (json[i] != ':') {
                        throw new InvalidJsonException("Expected ':' at column " + i + "!");
                    }
                    ++i; // Skip the ':'
                    SkipWhitespace(json, ref i);
                    var value = ReadValue(json, ref i);
                    obj.Add((string)key, value);

                    SkipWhitespace(json, ref i);

                    if (json[i] == ',') {
                        i++; // Skip the ','
                    } else {
                        break;
                    }
                }
            }
            if (json[i] != '}') {
                throw new InvalidJsonException("Expected closing object token at column " + i + "!");
            }

            i++; // Skip the '}'

            return obj;
        }
开发者ID:grahamboree,项目名称:voorhees,代码行数:39,代码来源:JsonReader.cs

示例4: KVPCollection

    public void KVPCollection()
    {
        JsonValue test = new JsonValue() {
            {"one", 1},
            {"two", 2},
            {"three", 3},
        };

        test.Remove(new KeyValuePair<string, JsonValue>("one", 1));
        Assert.That(test.Count, Is.EqualTo(2));
        Assert.Throws<KeyNotFoundException>(() => test["one"].ToString());

        KeyValuePair<string, JsonValue>[] dest = new KeyValuePair<string, JsonValue>[2];
        test.CopyTo(dest, 0);
        for (int i = 0; i < 2; ++i) {
            Assert.That(test[dest[i].Key].Equals(dest[i].Value));
        }

        Assert.That(test.Contains(new KeyValuePair<string, JsonValue>("two", 2)));

        test.Add(new KeyValuePair<string, JsonValue>("four", 4));

        Assert.That(test.ContainsKey("four"), Is.True);
        Assert.That((int)test["four"], Is.EqualTo(4));
    }
开发者ID:grahamboree,项目名称:voorhees,代码行数:25,代码来源:JsonValueTest.cs

示例5: IDictionary

    public void IDictionary()
    {
        JsonValue test = new JsonValue() {
            {"one", 1},
            {"two", 2},
            {"three", 3}
        };

        Assert.That((int)test["one"], Is.EqualTo(1));
        test["one"] = 5;
        Assert.That((int)test["one"], Is.EqualTo(5));
        test["one"] = 1;

        var keys = test.Keys;
        Assert.That(keys.Count, Is.EqualTo(3));
        Assert.That(keys.Contains("one"), Is.True);
        Assert.That(keys.Contains("two"), Is.True);
        Assert.That(keys.Contains("three"), Is.True);

        var values = test.Values;
        Assert.That(values.Count, Is.EqualTo(3));
        var intvalues = values.Select(x => (int)x).ToArray();
        Array.Sort(intvalues);
        for (int i = 0; i < 3; i++) {
            Assert.That(intvalues[i], Is.EqualTo(i + 1));
        }

        test.Add("four", 4);
        Assert.That(test.Count, Is.EqualTo(4));
        Assert.That((int)test["four"], Is.EqualTo(4));

        JsonValue val;
        Assert.That(test.TryGetValue("one", out val), Is.True);
        Assert.That((int)val, Is.EqualTo(1));
        Assert.That(test.TryGetValue("seven", out val), Is.False);
        Assert.That(val, Is.Null);

        Assert.That(test.ContainsKey("four"), Is.True);
        test.Remove("four");
        Assert.That(test.Count, Is.EqualTo(3));
        Assert.That(test.ContainsKey("four"), Is.False);
    }
开发者ID:grahamboree,项目名称:voorhees,代码行数:42,代码来源:JsonValueTest.cs


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