NibblyPig
NibblyPig

Reputation: 52922

JSON.NET won't deserialize into my object, throws an exception

My JSON (myString) looks like this:

"http://d.opencalais.com/dochash-1/0701d73f-2f99-39e1-8c29-e61ee8bf3238/cat/1":
{
  "_typeGroup": "topics",
  "category": "http://d.opencalais.com/cat/Calais/Law_Crime",
  "classifierName": "Calais",
  "categoryName": "Law_Crime",
  "score": 0.869
}

I am trying to deserialise the above exact string into an object:

public class OpenCalaisResult
{
    public string _typeGroup {get; set; }
    public string category { get; set; }
    public string categoryName { get; set; }
    public string classifierName { get; set; }
    public decimal score { get; set; }
}

I am trying this code:

OpenCalaisResult myObject = (OpenCalaisResult)JsonConvert.DeserializeObject(myString, typeof(OpenCalaisResult), settings);

I get an exception:

{"Error converting value \"http://d.opencalais.com/dochash-1/0701d73f-2f99-39e1-8c29-e61ee8bf3238/cat/1\" to type 'MyApp.Parsers.JsonTypes.OpenCalaisResult'. Path '', line 1, position 78."}

Any idea what I am doing wrong?

Upvotes: 0

Views: 1166

Answers (2)

Shafqat Masood
Shafqat Masood

Reputation: 2570

your json should be like this

 {
 "http://d.opencalais.com/dochash-1/0701d73f-2f99-39e1-8c29-e61ee8bf3238/cat/1":
  {
    "_typeGroup": "topics",
    "category": "http://d.opencalais.com/cat/Calais/Law_Crime",
    "classifierName": "Calais",
    "categoryName": "Law_Crime",
    "score": 0.869
   }
  }

Upvotes: 5

softwarebear
softwarebear

Reputation: 451

http://www.json.org/

Shows objects have the form ...

{ ... }

The form of your JSON is ...

x : { ... }

I'm guessing that 'x' is the type of the class being [de]serialised ... I don't believe it's supported by your library.

JSON support is a bit random.

Upvotes: 0

Related Questions