4

I am using the following approach to convert most of my API JSON results into an object:

public void ExpandoObject()
{
    var sampleDATA = Sample.Create();
    var json = JsonConvert.SerializeObject(sampleDATA);

    var expConverter = new ExpandoObjectConverter();
    dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json, expConverter);

    var a = obj.A;

    var b = obj.B;

    var c = obj.C; //and so on...
}

However, I run into a strange situation with this format of JSON...

[
    {
        "id": 42,
        "name": "example name",
        "member_count": 42,
        "created_date": "example created_date",
        "last_update": "example last_update",
        "last_reset": "example last_reset"
    }
]

Because it is an array, how can I access the items, the ExpandoObject is supposed to be an IDictionary of sorts.

Anyone had experience with this?

1 Answer 1

8

Use List<ExpandoObject> when deserializing:

var expConverter = new ExpandoObjectConverter();
dynamic obj = JsonConvert.DeserializeObject<List<ExpandoObject>>(json, expConverter);

Your obj variable will be list of expando objects that you can iterate.

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

2 Comments

What is expConverter. What does it do?
It is ExpandoObjectConverter. I missed it when posting the answer. It is edited now.

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.