-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,);
4
  • 6
    Your properties have a Number suffix in their name, your json does not. Commented Oct 28, 2019 at 22:06
  • 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. Commented Oct 28, 2019 at 22:10
  • 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: newtonsoft.com/json Commented Oct 28, 2019 at 22:28
  • 1
    Possible duplicate of How to parse json object in C#? However, note that deserializing is not the same as parsing Commented Oct 28, 2019 at 22:40

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.