-1

OK - I'm throwing in the towel on this. I've spent half day trying to parse a simple JSON object and none of the (at least 4) methods I've tried have worked. What the heck am I missing? The value of the JSON string is:

{
  "CurrentVersion": "1.1.11",
  "ID": "1",
  "InstallerVersion": "1.0.0",
  "LastVersion": "1.1.10",
  "Name": "kart"
}

and no matter what I use (JsonConvert.DeserializeObject, JavaScriptSerializer.Deserialize or JsonSerializer.Deserialize) the result is the same. The first, third and fourth values are null. I've tried changing those values in the JSON to strings, e.g.:

{
  "CurrentVersion": "one",
  "ID": "1",
  "InstallerVersion": "two",
  "LastVersion": "three",
  "Name": "kart"
}

I tried changing the object value to Version (it's currently just string); nothing works. There's got to be something simple I'm missing, but what?

Here's a picture of what I've tried so far: JSON Hell!!

For simplicity here's the code I've tried:

CurrentVersion currentVersion = js.Deserialize<CurrentVersion>(jsonResult);
currentVersion = JsonConvert.DeserializeObject<CurrentVersion>(jsonResult);
var version = System.Web.Script.Serialization.JavaScriptSerializer.Deserialize<currentVersion>(jsonResult);
currentVersion = JsonSerializer.Deserialize<CurrentVersion>(jsonResult,);
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Ben_G
  • 770
  • 2
  • 8
  • 30
  • 6
    Your properties have a `Number` suffix in their name, your json does not. – Jonathon Chase Oct 28 '19 at 22:06
  • I had many similar problems until I bailed on the built-in JSON support and went to Json.NET from Newtonsoft - made life *so* much better. Ref: https://www.newtonsoft.com/json – Steve Friedl Oct 28 '19 at 22:28
  • 1
    Possible duplicate of [How to parse json object in C#?](https://stackoverflow.com/questions/12402085/how-to-parse-json-object-in-c) However, note that *deserializing* is not the same as *parsing* – Ňɏssa Pøngjǣrdenlarp Oct 28 '19 at 22:40
  • Maybe it is just difference between names? I mean e.g. InstallerVersionNumber vs InstallerVersion. Try using this same naming convention. What I mean, you should call your model proporties in this same way as they are in .Json file. – cebilon123 Oct 28 '19 at 22:10

1 Answers1

0

Soooooo stupid. I've considered down-voting my own question. I was going to delete it (you know - shred the evidence of my stupidity), but I figured might as well let it stay here. Maybe someone else will make the same mistake.

Jonathan Chase had it - the names in my class didn't match the values in the JSON.

Ben_G
  • 770
  • 2
  • 8
  • 30