从C#的JSON到ExpandoObject转换

时间:2018-08-06 12:47:21

标签: c# json json.net expandoobject

UnboundLocalError: local variable 'var1' referenced before assignment

这是我的JSON格式。如何在C#中转换为ExpandoObject? 这是我的代码,

{  
"Jhone":[  
  {  
     "Key":"Employeename",
     "Value":"Jhone"
  },
  {  
     "Key":"Address",
     "Value":[  
        {  
           "Key":"City",
           "Value":"Newyork"
        },
        {  
           "Key":"Country",
           "Value":"USA"
        }
     ]
  }
],
"Mohamed":[  
  {  
     "Key":"Employeename",
     "Value":"Mohamed"
  },
  {  
     "Key":"Address",
     "Value":[  
        {  
           "Key":"City",
           "Value":"Delhi"
        },
        {  
           "Key":"Country",
           "Value":"india"
        }
     ]
  }
]
}

1 个答案:

答案 0 :(得分:0)

也许您可以使用此扩展方法

public static ExpandoObject Expando(this IEnumerable<KeyValuePair<string, object>> dictionary)
{
    var expando = new ExpandoObject();
    var expandoDic = (IDictionary<string, object>)expando;
    foreach (var item in dictionary)
    {
        expandoDic.Add(item);
    }
    return expando;
}

方法返回带有键,值列表的ExpandoObject