Matt
Matt

Reputation: 2863

Convert JSON String to List of Dynamic Objects in C#

Ive got a JSON string im sending from my web client to my webapi. This JSON string kind of dynamically formed.

So I want to know if there is a way if I can convert this JSON string into a list/array of dynamic objects. So can handle it along the same line as:

var DynamicArray = WhatEverJSONConvertor(JSONString);

for (int i = 0; i < DynamicArray.length; i++)
{
      Console.WriteLine(DynamicArray[i].AFieldInTheObject);
}

Is this possible? What JSON convertor would I use this to accomplish this?

Thanks

Upvotes: 0

Views: 3231

Answers (1)

DarthVader
DarthVader

Reputation: 55022

Why dont you use JSon.net and for your json response use dynamic :

dyanmic [] jsonresponseArray= WhatEverJSONConvertor(JSONString);

for (int i = 0; i < DynamicArray.length; i++)
{
      Console.WriteLine(jsonresponseArray[i].AFieldInTheObject);
}

....
public dynamic[] WhatEverJSONConvertor(string json){
   // parse and create a dynamic type object
}

You need to ensure if the field exists, such as extension method could do.

Upvotes: 1

Related Questions