當前位置: 首頁>>代碼示例>>C#>>正文


C# JTokenReader.Read方法代碼示例

本文整理匯總了C#中Newtonsoft.Json.Linq.JTokenReader.Read方法的典型用法代碼示例。如果您正苦於以下問題:C# JTokenReader.Read方法的具體用法?C# JTokenReader.Read怎麽用?C# JTokenReader.Read使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Newtonsoft.Json.Linq.JTokenReader的用法示例。


在下文中一共展示了JTokenReader.Read方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: GetCharacterFromSource

        /// <summary>
        ///     Extrae un personaje de la fuente de datos en JSON.
        /// </summary>
        /// <param name="name">
        ///     El nombre del pesonaje a extraer de la fuente de datos.
        /// </param>
        /// <returns>
        ///     Un objeto Character cuyo nombre coincide con el solicitado.
        /// </returns>
        /// <pre>true</pre>
        /// <post>(this.Contains(name) => (returnValue != null) && (returnValue.Name.Equals(name)))</post>
        public FullCharacter GetCharacterFromSource(string name)
        {
            FullCharacter character = null;

            // Obtenemos un lector de tokens de Json. Para ello creamos primero un stream de la
            // cadena json, luego creamos un lector de texto usando ese stream y por último creamos
            // un lector de tokens utilizando el lector de texto y lo inicializamos
            JsonReader reader = new JsonTextReader(new StringReader(jsonString));
            JTokenReader tokenreader = new JTokenReader(JToken.ReadFrom(reader));
            tokenreader.Read();

            // Obtenemos el token cuyo nombre corresponde al deseado, utilizando para ello una
            // expresión JSonPath
            // Para más información sobre JPath: http://goessner.net/articles/JsonPath/
            JToken token = tokenreader.CurrentToken.SelectToken("$.[?(@.name=='" + name + "')]", false);

            // Si existe un token con dihco nombre, existe el personaje buscado
            if (token != null)
            {
                // Creamos un nuevo objeto de tipo Character e incrementamos el correspondiente
                // contador
                character = new FullCharacter();
                createdCharacters++;

                // Extraemos el valor de la Propiedad Nombre del correspondiente token
                character.Name = token.SelectToken("name").Value<String>();

                // Extraemos el valor de la Propiedad Nombre del correspondiente token
                character.Description = token.SelectToken("description").Value<String>();
                IEnumerable<JToken> relatedCharactersTokens = token.SelectToken("related_characters").Values<JToken>();

                // Construimos la colección de personajes relacionados con la colección de tokens JSON
                FillRelatedCharacters(character, relatedCharactersTokens);
            } // if

            // Cerramos los lectores que estén abiertos
            tokenreader.Close();
            reader.Close();

            return character;
        }
開發者ID:algorri94,項目名稱:dissoftware,代碼行數:52,代碼來源:GetJsonService.cs

示例2: JTokenReader

    public void JTokenReader()
    {
      PersonRaw raw = new PersonRaw
      {
        FirstName = "FirstNameValue",
        RawContent = new JRaw("[1,2,3,4,5]"),
        LastName = "LastNameValue"
      };

      JObject o = JObject.FromObject(raw);

      JsonReader reader = new JTokenReader(o);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Raw, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);

      Assert.IsFalse(reader.Read());
    }
開發者ID:Jessecar96,項目名稱:Newtonsoft.Json,代碼行數:39,代碼來源:JObjectTests.cs

示例3: InitialPath_ArrayBase_ArrayToken

        public void InitialPath_ArrayBase_ArrayToken()
        {
            JArray a = new JArray
            {
                1, 2
            };

            JTokenReader reader = new JTokenReader(a, "[0]");

            Assert.AreEqual("[0]", reader.Path);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual("[0]", reader.Path);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual("[0][0]", reader.Path);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual("[0][1]", reader.Path);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual("[0]", reader.Path);

            Assert.IsFalse(reader.Read());
            Assert.AreEqual("[0]", reader.Path);
        }
開發者ID:michal2229,項目名稱:cs-json-dane-pogodowe-openweathermap,代碼行數:26,代碼來源:JTokenReaderTest.cs

示例4: YahooFinance

        public void YahooFinance()
        {
            JObject o =
                new JObject(
                    new JProperty("Test1", new DateTime(2000, 10, 15, 5, 5, 5, DateTimeKind.Utc)),
                    new JProperty("Test2", new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0))),
                    new JProperty("Test3", "Test3Value"),
                    new JProperty("Test4", null)
                    );

            using (JTokenReader jsonReader = new JTokenReader(o))
            {
                IJsonLineInfo lineInfo = jsonReader;

                jsonReader.Read();
                Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);
                Assert.AreEqual(false, lineInfo.HasLineInfo());

                jsonReader.Read();
                Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
                Assert.AreEqual("Test1", jsonReader.Value);
                Assert.AreEqual(false, lineInfo.HasLineInfo());

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Date, jsonReader.TokenType);
                Assert.AreEqual(new DateTime(2000, 10, 15, 5, 5, 5, DateTimeKind.Utc), jsonReader.Value);
                Assert.AreEqual(false, lineInfo.HasLineInfo());
                Assert.AreEqual(0, lineInfo.LinePosition);
                Assert.AreEqual(0, lineInfo.LineNumber);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
                Assert.AreEqual("Test2", jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Date, jsonReader.TokenType);
                Assert.AreEqual(new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0)), jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
                Assert.AreEqual("Test3", jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.String, jsonReader.TokenType);
                Assert.AreEqual("Test3Value", jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
                Assert.AreEqual("Test4", jsonReader.Value);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.Null, jsonReader.TokenType);
                Assert.AreEqual(null, jsonReader.Value);

                Assert.IsTrue(jsonReader.Read());
                Assert.AreEqual(JsonToken.EndObject, jsonReader.TokenType);

                Assert.IsFalse(jsonReader.Read());
                Assert.AreEqual(JsonToken.None, jsonReader.TokenType);
            }

            using (JsonReader jsonReader = new JTokenReader(o.Property("Test2")))
            {
                Assert.IsTrue(jsonReader.Read());
                Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
                Assert.AreEqual("Test2", jsonReader.Value);

                Assert.IsTrue(jsonReader.Read());
                Assert.AreEqual(JsonToken.Date, jsonReader.TokenType);
                Assert.AreEqual(new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0)), jsonReader.Value);

                Assert.IsFalse(jsonReader.Read());
                Assert.AreEqual(JsonToken.None, jsonReader.TokenType);
            }
        }
開發者ID:michal2229,項目名稱:cs-json-dane-pogodowe-openweathermap,代碼行數:75,代碼來源:JTokenReaderTest.cs

示例5: InitialPath_ArrayBase_PropertyToken

        public void InitialPath_ArrayBase_PropertyToken()
        {
            JObject o = new JObject
            {
                { "prop1", true }
            };

            JTokenReader reader = new JTokenReader(o, "[0]");

            Assert.AreEqual("[0]", reader.Path);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual("[0]", reader.Path);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual("[0].prop1", reader.Path);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual("[0].prop1", reader.Path);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual("[0]", reader.Path);

            Assert.IsFalse(reader.Read());
            Assert.AreEqual("[0]", reader.Path);
        }
開發者ID:michal2229,項目名稱:cs-json-dane-pogodowe-openweathermap,代碼行數:26,代碼來源:JTokenReaderTest.cs

示例6: ReadBytesFailure

        public void ReadBytesFailure()
        {
            ExceptionAssert.Throws<JsonReaderException>(() =>
            {
                JObject o =
                    new JObject(
                        new JProperty("Test1", 1)
                        );

                using (JTokenReader jsonReader = new JTokenReader(o))
                {
                    jsonReader.Read();
                    Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);

                    jsonReader.Read();
                    Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
                    Assert.AreEqual("Test1", jsonReader.Value);

                    jsonReader.ReadAsBytes();
                }
            }, "Error reading bytes. Unexpected token: Integer. Path 'Test1'.");
        }
開發者ID:michal2229,項目名稱:cs-json-dane-pogodowe-openweathermap,代碼行數:22,代碼來源:JTokenReaderTest.cs

示例7: ReadBytes

        public void ReadBytes()
        {
            byte[] data = Encoding.UTF8.GetBytes("Hello world!");

            JObject o =
                new JObject(
                    new JProperty("Test1", data)
                    );

            using (JTokenReader jsonReader = new JTokenReader(o))
            {
                jsonReader.Read();
                Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);

                jsonReader.Read();
                Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
                Assert.AreEqual("Test1", jsonReader.Value);

                byte[] readBytes = jsonReader.ReadAsBytes();
                Assert.AreEqual(data, readBytes);

                Assert.IsTrue(jsonReader.Read());
                Assert.AreEqual(JsonToken.EndObject, jsonReader.TokenType);

                Assert.IsFalse(jsonReader.Read());
                Assert.AreEqual(JsonToken.None, jsonReader.TokenType);
            }
        }
開發者ID:michal2229,項目名稱:cs-json-dane-pogodowe-openweathermap,代碼行數:28,代碼來源:JTokenReaderTest.cs

示例8: ReadLineInfo

        public void ReadLineInfo()
        {
            string input = @"{
  CPU: 'Intel',
  Drives: [
    'DVD read/writer',
    ""500 gigabyte hard drive""
  ]
}";

            JObject o = JObject.Parse(input);

            using (JTokenReader jsonReader = new JTokenReader(o))
            {
                IJsonLineInfo lineInfo = jsonReader;

                Assert.AreEqual(jsonReader.TokenType, JsonToken.None);
                Assert.AreEqual(0, lineInfo.LineNumber);
                Assert.AreEqual(0, lineInfo.LinePosition);
                Assert.AreEqual(false, lineInfo.HasLineInfo());
                Assert.AreEqual(null, jsonReader.CurrentToken);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.StartObject);
                Assert.AreEqual(1, lineInfo.LineNumber);
                Assert.AreEqual(1, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());
                Assert.AreEqual(o, jsonReader.CurrentToken);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName);
                Assert.AreEqual(jsonReader.Value, "CPU");
                Assert.AreEqual(2, lineInfo.LineNumber);
                Assert.AreEqual(7, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());
                Assert.AreEqual(o.Property("CPU"), jsonReader.CurrentToken);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
                Assert.AreEqual(jsonReader.Value, "Intel");
                Assert.AreEqual(2, lineInfo.LineNumber);
                Assert.AreEqual(15, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());
                Assert.AreEqual(o.Property("CPU").Value, jsonReader.CurrentToken);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName);
                Assert.AreEqual(jsonReader.Value, "Drives");
                Assert.AreEqual(3, lineInfo.LineNumber);
                Assert.AreEqual(10, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());
                Assert.AreEqual(o.Property("Drives"), jsonReader.CurrentToken);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.StartArray);
                Assert.AreEqual(3, lineInfo.LineNumber);
                Assert.AreEqual(12, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());
                Assert.AreEqual(o.Property("Drives").Value, jsonReader.CurrentToken);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
                Assert.AreEqual(jsonReader.Value, "DVD read/writer");
                Assert.AreEqual(4, lineInfo.LineNumber);
                Assert.AreEqual(22, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());
                Assert.AreEqual(o["Drives"][0], jsonReader.CurrentToken);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
                Assert.AreEqual(jsonReader.Value, "500 gigabyte hard drive");
                Assert.AreEqual(5, lineInfo.LineNumber);
                Assert.AreEqual(30, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());
                Assert.AreEqual(o["Drives"][1], jsonReader.CurrentToken);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.EndArray);
                Assert.AreEqual(3, lineInfo.LineNumber);
                Assert.AreEqual(12, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());
                Assert.AreEqual(o["Drives"], jsonReader.CurrentToken);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.EndObject);
                Assert.AreEqual(1, lineInfo.LineNumber);
                Assert.AreEqual(1, lineInfo.LinePosition);
                Assert.AreEqual(true, lineInfo.HasLineInfo());
                Assert.AreEqual(o, jsonReader.CurrentToken);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.None);
                Assert.AreEqual(null, jsonReader.CurrentToken);

                jsonReader.Read();
                Assert.AreEqual(jsonReader.TokenType, JsonToken.None);
                Assert.AreEqual(null, jsonReader.CurrentToken);
            }
        }
開發者ID:michal2229,項目名稱:cs-json-dane-pogodowe-openweathermap,代碼行數:99,代碼來源:JTokenReaderTest.cs

示例9: ReadBytesFailure

    public void ReadBytesFailure()
    {
      JObject o =
        new JObject(
          new JProperty("Test1", 1)
        );

      using (JTokenReader jsonReader = new JTokenReader(o))
      {
        jsonReader.Read();
        Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);

        jsonReader.Read();
        Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
        Assert.AreEqual("Test1", jsonReader.Value);

        jsonReader.ReadAsBytes();
      }
    }
開發者ID:thirumg,項目名稱:Avro.NET,代碼行數:19,代碼來源:JTokenReaderTest.cs


注:本文中的Newtonsoft.Json.Linq.JTokenReader.Read方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。