1

I'm trying a class to read from a JSON string using JsonTextReader (i need to read in a streaming pov, without loading the whole structure into memory). But it seems that JsonTextReader does not work well with arrays or I'm doing something wrong, since there are values that are not Read().

My code:

while (jsonReader.Read())
{
    if (jsonReader.Value != null)
        Console.WriteLine("Token: {0}, Value: {1}", jsonReader.TokenType, jsonReader.Value);
    else
        Console.WriteLine("Token: {0}", jsonReader.TokenType);
}

My JSON file:

{
    "employees": [
        {
            "firstName": "John",
            "lastName": "Doe"
        },
        {
            "firstName": "Anna",
            "lastName": "Smith"
        },
        {
            "firstName": "Peter",
            "lastName": "Jones"
        }
    ]
}

The output:

Token: StartObject
Token: StartArray
Token: StartObject
Token: PropertyName, Value: firstName
Token: PropertyName, Value: lastName
Token: EndObject
Token: StartObject
Token: PropertyName, Value: firstName
Token: PropertyName, Value: lastName
Token: EndObject
Token: StartObject
Token: PropertyName, Value: firstName
Token: PropertyName, Value: lastName
Token: EndObject
Token: EndArray
Token: EndObject

Please note that the content like Jonh, Anna, Doe,... aren't showed.

Any idea how could I solve this? I'm avoiding serialization\deserialization solutions or LINQ, since I can't load the whole structure into memory.

Thanks

Ricardo
  • 99
  • 2
  • 10
  • Are you needing to `Deserialize the JSON string` ..? have you looked at the following [JSON .NET Library](http://json.codeplex.com/releases/view/37810) | take a look at this `SO previous posting sounds / looks like something that you need http://stackoverflow.com/questions/2859753/what-is-simpliest-c-sharp-function-to-parse-json-string-into-object – MethodMan Apr 08 '13 at 14:40
  • It is working fine for me. Full program [here](http://pastebin.com/DabutHeq). Output [here](http://pastebin.com/xahzF57E). Using Json.Net v4.5.11, .Net framework v4.5. Are there some details missing from the question? – Brian Rogers Apr 08 '13 at 16:24
  • Brian, your sample works, it seems to be the "". I will to do some more tests, but it's a good starting point, thanks. – Ricardo Apr 09 '13 at 14:48
  • Voting to close this old question since the original problem cannot be reproduced (working fiddle [here](https://dotnetfiddle.net/93uUEo)) and the post was never updated with more details. – Brian Rogers Jan 19 '19 at 23:05

1 Answers1

-1

Looks like for properties, jsonReader.Value returns property name, when ToString is called. You can try casting value to something like JsonProperty type and see if it holds a value of the property. I'm not sure what the exact type name is, but you can look it up in the debugger.

alex
  • 12,464
  • 3
  • 46
  • 67