简体   繁体   English

将JSON字符串转换为C#中的动态对象列表

[英]Convert JSON String to List of Dynamic Objects in C#

Ive got a JSON string im sending from my web client to my webapi. 我有一个JSON字符串,即时消息从我的Web客户端发送到我的webapi。 This JSON string kind of dynamically formed. 这种JSON字符串是动态形成的。

So I want to know if there is a way if I can convert this JSON string into a list/array of dynamic objects. 因此,我想知道是否可以将JSON字符串转换为动态对象的列表/数组。 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? 我将使用哪个JSON转换器完成此任务?

Thanks 谢谢

Why dont you use JSon.net and for your json response use dynamic : 你为什么不使用JSon.net并为json响应使用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. 您需要确保该字段是否存在,例如扩展方法可以做到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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