Newtonsoft.Json.Linq.JTokenReader.Read C# (CSharp) Method

Read() public method

Reads the next JSON token from the stream.
public Read ( ) : bool
return bool
    public override bool Read()
    {
      if (CurrentState != State.Start)
      {
        JContainer container = _current as JContainer;
        if (container != null && _parent != container)
          return ReadInto(container);
        else
          return ReadOver(_current);
      }

      SetToken(_current);
      return true;
    }

Usage Example

Esempio n. 1
0
        /// <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;
        }
All Usage Examples Of Newtonsoft.Json.Linq.JTokenReader::Read