将 PHP 数组转换为 C# 哈希表


casting php arrays to c# hashtables

我有一些多维的php数组被传递给我的c#应用程序。 要在 c# 端提取值,我必须执行以下操作:

String example = (string)((Hashtable)((Hashtable)example_info["FirstLevel"])["SecondLevel"])["example_value"];

我将如何消除将每个维度显式转换为哈希表的需要? 我是否需要一个递归函数来构建某种 List 对象example_info,或者我不应该使用哈希表?

在这里,使用这个:

    public Dictionary<string, object> Parse(string array)
    { 
        Dictionary<string, object> result = new Dictionary<string, object>();
        Newtonsoft.Json.Linq.JObject obj = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(array);
        foreach (KeyValuePair<string, Newtonsoft.Json.Linq.JToken> kvp in obj)
        {
            if (kvp.Value.ToString().Contains('{'))
            {
                result.Add(kvp.Key, Parse(kvp.Value.ToString().Replace("[", "").Replace("]", "")));
            }
            else
            {
                result.Add(kvp.Key, kvp.Value.ToString());
            }
        }
        return result;
    }