1

I have class which return json string but I want to deserilize it into C# List Objects. My current code look like this

public class JsonBuilder
    {
        public static string BuildJson(DateTime fromDate, DateTime toDate)
        {
            var list = new List<dynamic>();

           // create list with json object from service

            var jsonObjList = JsonConvert.SerializeObject(list);
            var des = (List<JsonObject>)JsonConvert.DeserializeObject(jsonObjList, typeof(List<JsonObject>));

            return JsonConvert.SerializeObject(list);
        }

Exception thrown when it tries to deserialize the "serialized" json string

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: Error converting value 


InnerException:

{"Could not cast or convert from System.String to MvcWebApp.Models.JsonObject."}

3 Answers 3

1

Have you tried this:

var des = (List<DeserializeObjects>)JsonConvert.DeserializeObject(jsonObjList, jsonObjList.GetType()));

or this:

var des = (List<DeserializeObjects>)JsonConvert.DeserializeObject(jsonObjList, typeof(List<dynamic>));

else this post could also help you to achieve your goal:

Deserialize json object into dynamic object using Json.net

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

2 Comments

Unfortunately, none of the trick work for me. Any suggestion?
I found a solution from the link you have given. Thanks a lot.
0

try this

 var jsonObjList = JsonConvert.SerializeObject(list);
 dynamic resultList = JsonConvert.DeserializeObject(jsonObjList);

Comments

0

I would try with this class

public class Author
{
    public int id { get; set; }
    public string slug { get; set; }
    public string name { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string nickname { get; set; }
    public string url { get; set; }
    public string description { get; set; }
}

public class CustomFields
{
    public List<string> tags { get; set; }
}

public class Post
{
    public int id { get; set; }
    public string type { get; set; }
    public string slug { get; set; }
    public string url { get; set; }
    public string status { get; set; }
    public string title { get; set; }
    public string title_plain { get; set; }
    public string content { get; set; }
    public string excerpt { get; set; }
    public string date { get; set; }
    public string modified { get; set; }
    public List<object> categories { get; set; }
    public List<object> tags { get; set; }
    public Author author { get; set; }
    public List<object> comments { get; set; }
    public List<object> attachments { get; set; }
    public int comment_count { get; set; }
    public string comment_status { get; set; }
    public CustomFields custom_fields { get; set; }
}

public class YourObject
{
    public string status { get; set; }
    public int count { get; set; }
    public int count_total { get; set; }
    public int pages { get; set; }
    public List<Post> posts { get; set; }
}

Then, you deserialize with this :

var yourObject = JsonConvert.DeserializeObject<YourObject>(json);

Comments

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.