1
{
    "_id": "underscore",
    "_rev": "136-824a0ef7436f808755f0712c3acc825f",
    "name": "underscore",
    "description": "JavaScript's functional programming helper library.",
    "dist-tags": {},
    "versions": {
        "1.0.3": {
            "name": "xxx",
            "description": "xxx"
        },
        "1.0.4": {},
        "1.1.0": {}
    }
}

I would like to retrieve the latest version(1.1.0) from the json file. However, it always gives out me errors of "can not deserialize json object into type RootObject

Here is my class

public class versions
{
    public string name { get; set; }
    public string description { get; set; }
}

public class RootObject
{
    public List<versions> vs { get; set; }
}

And here is where I used it

RootObject[] dataset = JsonConvert.DeserializeObject<RootObject[]>(json);

Any idea. Many thankx

Update:

I have updated the JSON file format, but some problem..

1
  • what are the problems that you have after the update? Commented Sep 5, 2013 at 10:24

3 Answers 3

2

I think the problem is, that in JSON you have to quote all "field"/attribute names. (Thats a difference from standard Javascript-Notation, where you can have unquoted attributes).

So, your file should be like:

{
  "_id" : "underscore",
  "versions": {
    "1.0.3" : {
       "name": "xxx",
       "description": "xxx"
    }
}

Note that {1.0.3: { name: "xxx" } } wouldn't be valid JavaScript either since '1.0.3' is an invalid identifier in JavaScript.

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

Comments

1

Looking at the JSON in your updated answer:

{
  "_id" : "underscore",
  "versions": {
    "1.0.3" : {
       "name": "xxx",
       "description": "xxx"
    },
   "1.0.4" : {
       "name": "xxx",
       "description": "xxx"
    }
}

This is still Invalid JSON - you have 4 opening { and only 3 closing }

you should use http://jsonlint.com/ - to validate your JSON and ensure it is Valid

Comments

1

I've fixed your json in question. Now for your real question

I would like to retrieve the latest version(1.1.0) from the json file. However, it always gives out me errors of "can not deserialize json object into type RootObject

You have property names like 1.0.3 that are unknown at compile time. So you can not deserialize them to a concrete class. You should handle them dynamically.

Try this:

var versions = JObject.Parse(json)["versions"]
               .Children()
               .Cast<JProperty>()
               .ToDictionary(c => c.Name, c => c.Value.ToObject<versions>());

2 Comments

Erorrs: can not inferred from the usage.. To dictionary and toObject is wrong, i assume
@user1851359 I tested it before posting. No errors. Have you included System.Linq ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.