6

I have a JSON like

{
  "40": {
    "name": "Team A vs Team B",
    "value": {
      "home": 1,
      "away": 0
    }
  },
  "45": {
    "name": "Team A vs Team C",
    "value": {
      "home": 2,
      "away": 0
    }
  },
  "50": {
    "name": "Team A vs Team D",
    "value": {
      "home": 0,
      "away": 2
    }
  }
}

So it's kind of list of matches. And I have the class to deserialize it into:

public class Match
{
    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }
    [JsonProperty(PropertyName = "value")]
    public Value Values { get; set; }
}

public class Value
{
    [JsonProperty(PropertyName = "home")]
    public int Home { get; set; }
    [JsonProperty(PropertyName = "away")]
    public int Away { get; set; }
}

I am trying to deserialize json like this:

var mList= JsonConvert.DeserializeObject<List<Match>>(jsonstr);

But i am getting exception:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ClassNameHere]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

If i change the code like:

var mList= JsonConvert.DeserializeObject(jsonstr);

Then it serializes but not as a list, as a object. How can I fix this?

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Sefa
  • 8,865
  • 10
  • 51
  • 82
  • Put a [] (bracket) from start and end of your jsonstr – agentpx Dec 25 '14 at 09:38
  • possible duplicate of [How can I parse a JSON string that would cause illegal C# identifiers?](http://stackoverflow.com/questions/24536533/how-can-i-parse-a-json-string-that-would-cause-illegal-c-sharp-identifiers) – Ilija Dimov Dec 25 '14 at 10:43

2 Answers2

9

In this case, you should ask Deserializer for IDictionary<string, Match>

var mList= JsonConvert.DeserializeObject<IDictionary<string, Match>>(jsonstr);

And the first element would have key "40" and the value will be the Match instance

Other words this part:

"40": {
    "name": "Team A vs Team B",
    "value": {
      "home": 1,
      "away": 0
    }

will result in KeyValuePair:

key - "40"
value - Match { Name = "Team",  ... }
Loofer
  • 6,841
  • 9
  • 61
  • 102
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • You are not suppose to change the document, but use the programming code to manage the document. – Sterling Diaz Mar 04 '16 at 17:03
  • @SterlingDiaz not sure what do you mean? I followed the JSON structure to show how to represent it in C#... so, I am really not sure what is your point here – Radim Köhler Mar 04 '16 at 18:19
4
"50": {
         "name": "Team A vs Team D",
         "value": {
                    "home": 0,
                    "away": 2
                  }
      }

The desirializer works correctly. In this json code value is an object. Try with this:

"50": {
         "name": "Team A vs Team D",
         "value": [{
                     "home": 0,
                     "away": 2
                  }]
      }

In this json code value is declared as an array of objects. Notice the [ and ]

mstrewe
  • 441
  • 3
  • 15