1

I am trying to pars json string:

{"name":,"age":30,"car":null}

First problem occurs when I parse json string into token.

JToken token = JToken.Parse(json);

After pasing json string json token has additional curly bracers.

{{"name":,"age":30,"car":null}}

Could you tell me why?

Next problem occurs when I try yo deserialize json string into .net object.

JToken.Parse() adds undefined to the name property:

{{"name": undefined,"age":30,"car":null}}

And deserialization fails. I expect that it will add null to the name property so json string can be converted to the .net object without exception. But, it doesn't and I am getting exception:

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: u. Path 'name', line 2, position 11.' 

How can I successfully parse such json string so it could be safely converted to the .net object?

Here is class structure for json string:

public class User
    {
        public string name { get; set; }
        public int age { get; set; }
        public object car { get; set; }
    }
Mrg Gek
  • 906
  • 2
  • 10
  • 31
  • 2
    `{"name":,"age":30,"car":null}` is not valid json - you cannot have a key without a value as shown (`"name"`). From that point you're likely hitting some edge cases in json.net that should otherwise be erroring out. – Joe Dec 29 '17 at 21:14
  • How could I validate this json to know in code that its invalid? – Mrg Gek Dec 29 '17 at 21:16
  • https://stackoverflow.com/questions/14977848/how-to-make-sure-that-string-is-valid-json-using-json-net this should your problem – RICKY KUMAR Dec 29 '17 at 21:25
  • 3
    @MrgGek IMO, the `JsonReaderException` is how you know its invalid. –  Dec 29 '17 at 21:30
  • As for double curlies, where does it add them? In the debugger viewing the object? That’s just presentation, nothing more. Elsewhere? Please edit and clarify – Sami Kuhmonen Dec 29 '17 at 21:38
  • Yes, in the debugger. Thanks for clarifying this. – Mrg Gek Dec 29 '17 at 22:02
  • What version of Json.NET are you using? – dbc Dec 29 '17 at 23:55
  • latest version on nuget – Mrg Gek Dec 30 '17 at 09:14

0 Answers0