65.9K
CodeProject 正在变化。 阅读更多。
Home

将 PHP 数组转换为 C# 字典

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2011年11月30日

CPOL
viewsIcon

31715

downloadIcon

103

PHP数组到C#字典的转换。

移除了之前的代码,那是一个非常糟糕且代码质量很差的想法。 现在我使用json_encode()将Json数组传递到Silverlight,并使用Newtonsoft.Json库和以下简单函数: 

     

  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;
        } 

© . All rights reserved.