21

I have the following dynamic object that I'm getting from a third party library:

IOrderStore os = ss.GetService<IOrderStore>();
IOrderInfo search = os.Orders.Where(t => t.Number == "test").FirstOrDefault();
IOrder orderFound = os.OpenOrder(search, true);

dynamic order = (dynamic)orderFound;
dynamic requirements = order.Title.Commitments[0].Requirements;

I need to parse it to a JSON string.

I tried this (using JSON.net):

string jsonString = JsonConvert.SerializeObject(requirements);
return jsonString;

But I get a seemingly corrupted JSON string, as below:

[{"$id":"1"},{"$id":"2"},{"$id":"3"},{"$id":"4"},{"$id":"5"},{"$id":"6"},{"$id":"7"},{"$id":"8"},{"$id":"9"},{"$id":"10"},{"$id":"11"},{"$id":"12"},{"$id":"13"},{"$id":"14"},{"$id":"15"}]

The object contains multiple properties, and not just the 'id'.

Any advice?

14
  • show your object in which you want to convert Commented Aug 8, 2016 at 9:07
  • 'string json = Newtonsoft.Json.JsonConvert.SerializeObject(foo);' Commented Aug 8, 2016 at 9:10
  • 1
    @user3378165 - yes please show the way Requirements is created because when just simply testing a list of dynamic objects and each with several fields they are all serialized Commented Aug 8, 2016 at 9:23
  • 1
    What you are trying to do looks perfectly correct. See this working example. We need to see the decalaration/definition of the Requirements property in order to help you. Commented Aug 8, 2016 at 9:47
  • 1
    @user3378165, sorry, but your edits don't help. What does Console.WriteLine(order.Title.Commitments[0].Requirements.GetType()); print? Show us that string, please. Also, you keep saying you are using a third party library. Which library? Is there documentation available? And most importantly: how you you know the returned JSON is "corrupted", as you say? What is your reference? What exactly do you expect to see? Commented Aug 8, 2016 at 10:22

2 Answers 2

15

Have you tried using var instead of dynamic?

// Use "var" in the declaration below.
var requirements = order.Title.Commitments[0].Requirements;
string jsonString = JsonConvert.SerializeObject(requirements);

When you only want to deserialize requirements without doing anything else with it then there is no need to use it dynamically.

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

4 Comments

Thank you for your answer but I got the same JSON, with only the IDs.. Any other idea?
Yeah, talk to the guys that maintain that 3rd party library. My guess is they made a mistake when subclassing DynamicObject.
This is what I think too... I emailed them, let's see.. Thank you for your assistance!
And did you check what does 'var' actually mean to the compiler??
7

Try using Convert.ToString() as following code to convert 'dynamic' object to 'string' -

dynamic order = (dynamic)orderFound;
dynamic requirements = order.Title.Commitments[0].Requirements;
string validString = Convert.ToString(requirements);

1 Comment

This works great in ASP.NET where var cannot be used

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.