3

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

2
  • could you please explain why do you need JSON Array as a dynamic variable ?
    – Vladimir
    Commented Jul 26, 2016 at 7:49
  • hi @Vladimir, see my edited post above , thank you
    – Webster
    Commented Jul 26, 2016 at 7:58

1 Answer 1

8

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

var list = JsonConvert.DeserializeObject<List<dynamic>>("[{ \"trx_id\": 1, \"bank_id\": 50 }]");
1
  • Man, thanks so much, I didn't expect it that way, so many distraction in my head @_@. Thanks dude
    – Webster
    Commented Jul 26, 2016 at 8:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.