Webster Webster - 1 year ago 124
JSON Question

How to store JSON array to dynamic List<>?

I'm just curious if there's a way to store JSON Array to dynamic List<> ?

For example :

Here's my JSON Array :

[ { "trx_id": 1, "bank_id": 50 } ]

I want to assign those JSON Array to a new List<>. Here's my code :

var dynObjectsList = new List<dynamic>(); // I want to assign both properties name and properties value to this variable

Here's the reason why i want it this way :

Basically, I'm building a client API to consume a web service. Let's say I successfully consumed web service A and web service B. I need to store the value returned from web service A and web service B in a separated List<>, so I can use Linq for JOIN-ing the List<>. I can store it easily if I got the model for each web service, but unfortunately I don't have it, so I need to build the List<> manually

I've been gooling it for awhile, but no luck for me.

Is there any way to do this?

Thank you

Answer Source

Using newtonsoft.json nuget package you could do the following:

var list = JsonConvert.DeserializeObject<List<dynamic>>("[{ \"trx_id\": 1, \"bank_id\": 50 }]");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download