5

I have this code:

string json2 = vc.Request(model.uri + "?fields=uri,transcode.status", "GET");
var deserial = JsonConvert.DeserializeObject<Dictionary<string, object>>(json2);
var transcode = deserial["transcode"];
var serial = JsonConvert.SerializeObject(transcode);
var deserial2 = JsonConvert.DeserializeObject<Dictionary<string, object>>(serial);
var upstatus = deserial2["status"].ToString();

The json I get from the server is:

{
    "uri": "/videos/262240241",
    "transcode": {
        "status": "in_progress"
    }
}

When running it on VS2017, it works.

But on VS2010 I get the following error:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.

I am using Newtonsoft.Json.

Any idea?

2
  • 1
    The answer is easy. please use visual studio json special past to create the unserialise object. Commented Mar 29, 2018 at 7:56
  • I asked a question further down, but: Are the framework versions you're using the same between the VS2017 and VS2010 project? Is the version of JSON.Net the same? Commented Mar 29, 2018 at 8:26

3 Answers 3

3

Your received json data is not a Dictionary<string, object>, it is a object

public class Transcode
{
    public string status { get; set; }
}

public class VModel
{
    public string uri { get; set; }
    public Transcode transcode { get; set; }
}

You can use this object:

var deserial = JsonConvert.DeserializeObject<VModel>(json2);

instead of:

var deserial = JsonConvert.DeserializeObject<Dictionary<string, object>>(json2);
Sign up to request clarification or add additional context in comments.

2 Comments

The json provided is also a valid Dictionary<string, object>
@AaYy Are the framework versions you're using the same between the VS2017 and VS2010 project? Is the version of JSON.Net the same?
2

The best answer was deleted for some reason, so i'll post it:

var deserial = JsonConvert.DeserializeObject<dynamic>(json2);
string upstatus = string.Empty;
upstatus = deserial.transcode.status.ToString();

Comments

2

If your model is not well defined or it is dynamic, then use:

var deserial = JsonConvert.DeserializeObject<dynamic>(json2);

or you can try to use:

JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(json2);

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.