反序列化/将 JSON 转换为 C# 列表

Deserialize/ convert JSON to C# list

我有 Web API,我在 Dynamic 中获取 JSON 对象,我需要转换为 C# 字符串、数组或列表,以便我可以相应地提取数据。我尝试了多种选择但没有得到任何结果。

我在 var g1 = JsonConvert.DeserializeObject(item) 处遇到异常,异常副本粘贴在下方

formStructureJsonBlob 第一个 json 对象是 'hiddenFields',json 输出粘贴在下面。

c#class

public dynamic formStructureJsonBlob { get; set; }


    public override Guid Execute()
    {
        try
        {
            foreach (var item in formStructureJsonBlob)
            {
                var g1 = JsonConvert.DeserializeObject(item); // getting error here 
            }

            //JObject j = new JObject();
            // JToken jt = j.SelectToken(formStructureJsonBlob.hiddenFields[0].name);
        }
        catch(Exception e)
        {
            Console.WriteLine(e);
        }

输出-json formStructureJsonBlob 类型

{
    "hiddenFields": [{
        "order": 0,
        "type": "hidden",
        "name": "formId",
        "value": "v1"
    },
    {
        "order": 0,
        "type": "hidden",
        "name": "consultationId",
        "value": "v2"
    },
    {
        "order": 0,
        "type": "hidden",
        "name": "clientId",
        "value": "v3"
    }
 ]
}

错误

{Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded 
method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject(string)' has 
some invalid arguments
 at CallSite.Target(Closure , CallSite , Type , Object )
 at Ant.Analysis.Infrastructure.Commands.SaveFormQuestionsAnswers.Execute() 
in C:\Developments\SaveFormQuestionsAnswers.cs:line 34}
  1. 如果您想遍历 hiddenFields,那么 formStructureJsonBlob.hiddenFields 应该可以工作,假设 formStructureJsonBlob 持有 Json。检查示例
  2. 您不需要反序列化。只需访问如下所示的属性。

示例,

public dynamic formStructureJsonBlob { get; set; }


public override Guid Execute()
{
    try
    {
        foreach (var item in formStructureJsonBlob.hiddenFields)
        {
Console.Write(item.order);
Console.Write(item.type);
Console.Write(item.name);
Console.Write(item.value); 
        }

    }
    catch(Exception e)
    {
        Console.WriteLine(e);
    }
}