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


C# JsonValue.TryGetValue方法代码示例

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


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

示例1: ParseNotStatusStreamLine

        /// <summary>
        /// Parse streamed JSON line (which is not a status)
        /// </summary>
        /// <param name="graph">JSON object graph</param>
        /// <param name="handler">result handler</param>
        internal static void ParseNotStatusStreamLine(JsonValue graph, IStreamHandler handler)
        {
            try
            {
                // element.foo() -> element.IsDefined("foo")

                // direct message
                JsonValue directMessage;
                if (graph.TryGetValue("direct_message", out directMessage))
                {
                    handler.OnStatus(new TwitterStatus(graph["direct_message"]));
                    return;
                }

                // delete
                JsonValue delete;
                if (graph.TryGetValue("delete", out delete))
                {
                    var timestamp = GetTimestamp(delete);
                    JsonValue status;
                    if (delete.TryGetValue("status", out status))
                    {
                        handler.OnMessage(new StreamDelete(
                            Int64.Parse(status["id_str"].AsString()),
                            Int64.Parse(status["user_id_str"].AsString()),
                            timestamp));
                        return;
                    }
                    if (delete.TryGetValue("direct_message", out directMessage))
                    {
                        handler.OnMessage(new StreamDelete(
                            Int64.Parse(directMessage["id_str"].AsString()),
                            Int64.Parse(directMessage["user_id"].AsString()),
                            timestamp));
                        return;
                    }
                }

                // scrub_geo
                JsonValue scrubGeo;
                if (graph.TryGetValue("scrub_geo", out scrubGeo))
                {
                    handler.OnMessage(new StreamScrubGeo(
                        Int64.Parse(scrubGeo["user_id_str"].AsString()),
                        Int64.Parse(scrubGeo["up_to_status_id_str"].AsString()),
                        GetTimestamp(scrubGeo)));
                    return;
                }

                // limit
                JsonValue limit;
                if (graph.TryGetValue("limit", out limit))
                {
                    handler.OnMessage(new StreamLimit(
                        limit["track"].AsLong(),
                        GetTimestamp(limit)));
                    return;
                }

                // withheld
                JsonValue statusWithheld;
                if (graph.TryGetValue("status_withheld", out statusWithheld))
                {
                    handler.OnMessage(new StreamWithheld(
                        statusWithheld["user_id"].AsLong(),
                        statusWithheld["id"].AsLong(),
                        ((JsonArray)statusWithheld["withheld_in_countries"]).Select(s => s.AsString()).ToArray(),
                        GetTimestamp(statusWithheld)));
                    return;
                }
                JsonValue userWithheld;
                if (graph.TryGetValue("user_withheld", out userWithheld))
                {
                    handler.OnMessage(new StreamWithheld(
                        userWithheld["id"].AsLong(),
                        ((JsonArray)statusWithheld["withheld_in_countries"]).Select(s => s.AsString()).ToArray(),
                        GetTimestamp(statusWithheld)));
                    return;
                }

                // disconnect
                JsonValue disconnect;
                if (graph.TryGetValue("disconnect", out disconnect))
                {
                    handler.OnMessage(new StreamDisconnect(
                        (DisconnectCode)disconnect["code"].AsLong(),
                        disconnect["stream_name"].AsString(),
                        disconnect["reason"].AsString(),
                        GetTimestamp(disconnect)));
                    return;
                }

                // stall warning
                JsonValue warning;
                if (graph.TryGetValue("warning", out warning))
//.........这里部分代码省略.........
开发者ID:karno,项目名称:Cadena,代码行数:101,代码来源:TwitterStreamParser.cs

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