-3

我的应用程序中有以下 JSON。

string json = @"{""dest"":[ { ""mode"": ""1"", ""test"":""test1,test,test2""},{ ""mode"": ""2"", ""test"": ""test3"" }]}";

使用以下方法获取dest I m的值。

 var json_serializer = new JavaScriptSerializer();
            Dictionary<string, object> dictionary = json_serializer.Deserialize<Dictionary<string, object>>(json);

     public Dictionary<string, object> GetObject(Dictionary<string, object> view, string name)
            {
                Dictionary<string, object> result = new Dictionary<string, object>();
                object value = null;
                foreach (KeyValuePair<string, object> pair in view)
                {
                    Type type = pair.Value.GetType();
                    if (pair.Key == name)
                    {
                        **Dictionary<string, object> child = (System.Collections.Generic.Dictionary<string, object>)pair.Value;**
                        result = GetObject(child, name);
                        if (result != null)
                        {
                            break;
                        }
                    }
                    else
                    {
                    }
                }
                return result;
            }

我在 Dictionary child = (System.Collections.Generic.Dictionary)pair.Value;行中遇到错误 . 错误提示“无法将 'System.Collections.ArrayList' 类型的对象转换为 'System.Collections.Generic.Dictionary`2[System.String,System.Object]'。”

任何人都知道如何解决这个问题?

4

1 回答 1

1

假设tset在您的数组中是一个错字,实际上是test,您可以使用具体类 ....

string json = @"{""dest"":[ { ""mode"": ""1"", ""test"":""test1,test,test2""},{ ""mode"": ""2"", ""test"": ""test3"" }]}";
var obj = new JavaScriptSerializer().Deserialize<MyObject>(json);

public class Dest
{
    public string mode { get; set; }
    public string test { get; set; }
}

public class MyObject
{
    public List<Dest> dest { get; set; }
}
于 2013-07-18T23:18:55.513 回答