6

please tell me, how do I get the json like this:

    dynamic packet = new ExpandoObject();
    packet.type = "somethink";
    packet.user = 12345;

    packet.nets[0].amout = 123;
    packet.nets[0].lower = 0;
    packet.nets[1].amout = 345;
    packet.nets[1].lower = 1;
    string input = Newtonsoft.Json.JsonConvert.SerializeObject(packet);

Its not workig, error: An unhandled exception of type "Microsoft.CSharp.RuntimeBinder.RuntimeBinderException" in System.Core.dll

For more information: "System.Dynamic.ExpandoObject" does not contain definitions of "nets"

Thanks.

3 Answers 3

13

It's the ExpandoObject who's a dynamic object. The rest of properties should be other ExpandoObject instances or regular objects, arrays, collections...

For example:

packet.nets = new[] 
{
     new { amount = 123, lower = 0 },
     new { amount = 345, lower = 1 }
}

Or:

packet.nets = new[]
{
     new Dictionary<string, int> { { "amount", 345 }, { "lower", 0 } },
     new Dictionary<string, int> { { "amount", 123 }, { "lower", 1 } }
}

There're many other approaches, including the use of instances of concrete classes.

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

Comments

4

Firstly, you need create nets in packet object, like this:

packet.nets = new dynamic[2];

And initialize the objects in nets, if you want with `ExpandoObject too:

packet.nets[0] = new ExpandoObject();
packet.nets[1] = new ExpandoObject();

Then is done, complete code:

dynamic packet = new ExpandoObject();
packet.type = "somethink";
packet.user = 12345;

packet.nets = new dynamic[2];

packet.nets[0] = new ExpandoObject();
packet.nets[0].amout = 123;
packet.nets[0].lower = 0;

packet.nets[1] = new ExpandoObject();
packet.nets[1].amout = 345;
packet.nets[1].lower = 1;            

string input = Newtonsoft.Json.JsonConvert.SerializeObject(packet);

Comments

1

You first need to declare nets. For example

packet.nets = new Dictionary<int, dynamic>();

Then you'll need to instantiate the instances of nets

packet.nets[0] = new {amount = 123, lower = 0};

The result being

dynamic packet = new ExpandoObject();
packet.type = "somethink";
packet.user = 12345;

packet.nets = new Dictionary<int, dynamic>();
packet.nets[0] = new { amount = 123, lower = 0 };
packet.nets[1] = new { amount = 345, lower = 1 };

4 Comments

Why the values should be dynamic?
@MatíasFidemraizer I guess they can be object, same difference. Your code automatic array initiation is probably better than declaring it as a Dictionary anyway. The only thing is you can remove the object part, the compiler will assume it. Not sure why you got the downvote, I upvoted.
Hey, yeah, I've turned the code to get type inference by usage. In my case I would declare values typed as dynamic if they should be accessed later. If it's just for serialization, who cares, object does the job well too
About the downvoter... maybe that user pressed the wrong button or who knows ;P

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.