简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM