c#动态对象变通方法

时间:2016-08-02 11:48:42

标签: c# asp.net .net json

我有一个在.NET 4.0+中运行的代码块,但我需要在仅支持.NET 3.5的SSIS包中使用此代码。问题是我无法使用4.0以下的动态对象。我无法找到解决方法,任何想法?

            string json = File.ReadAllText(@"C:json.txt");

            dynamic deserialisedJson = JsonConvert.DeserializeObject(json);

            var locations = new List<Location>();

            foreach (var root in deserialisedJson)
            {
                foreach (var state in root)
                {
                    foreach (var city in state)
                    {
                        foreach (var location in city)
                        {
                            Location loc = new Location();
                            loc.CafeId = location.First["cafeID"];
                            loc.CafeName = location.First["cafeName"];
                            loc.CafeState = location.First["cafeState"];
                            loc.CafeCity = location.First["cafeCity"];
                            loc.CafeStreetName = location.First["cafeStreetName"];
                            loc.CafeZip = location.First["cafeZip"];

                            locations.Add(loc);
                        }
                    }
                }
            }

更新 添加JSON模式

{
"AK": {
    "Anchorage": [{
        "Name": "John Doe",
        "Address": "123 Main St.",
        "City": "Anchorage",
        "State": "AK",
        "Zip": "12345"
    }],
    "Fairbanks": [{
        "Name": "Sally Smith",
        "Address": "987 Main St.",
        "City": "Fairbanks",
        "State": "AK",
        "Zip": "98765"
    }]
}

}

更新2

我正在尝试IEnumerable解决方法,但不确定正确的语法是什么,以便我可以获取所需的值:

string json = File.ReadAllText(@"C:json.txt");

            var deserialisedJson = (IEnumerable)JsonConvert.DeserializeObject(json);

            var locations = new List<Location>();

            foreach (var root in deserialisedJson)
            {
                foreach (var state in (IEnumerable)root)
                {
                    foreach (var city in (IEnumerable)state)
                    {
                        foreach (var location in (IEnumerable)city)
                        {
                            Location loc = new Location();

                            loc.Name = //What goes here???
                            loc.Address = //What goes here???
                            loc.City = //What goes here???
                            loc.State = //What goes here???
                            loc.Zip = //What goes here???

                            locations.Add(loc);
                        }
                    }
                }
            }

2 个答案:

答案 0 :(得分:0)

来自其他帖子 - Newtonsoft JSON Deserialize

class MyData
{
    public string t;
    public bool a;
    public object[] data;
    public string[][] type;
}

然后使用DeserializeObject的通用版本:

MyData tmp = JsonConvert.DeserializeObject<MyData>(json);
foreach (string typeStr in tmp.type[0])
{
    // Do something with typeStr
}

答案 1 :(得分:0)

如果没有json的例子,我只能推测 - 但看起来你已经了解了json的(相关)架构。你首先不需要动态,甚至在.net 4.0及更高版本中我建议不要使用动态。使用dynamic的代码通常较慢,更容易出错,并且比静态类型的代码更难调试,这不仅仅是因为编译时检查,还因为运行时的错误出现得更早。

从你有限的例子中,不知道First是什么东西,我觉得你可以做类似......

class LocationFromJson {
    public LocationContentsFromJson First;
}

class LocationContentsFromJson {
    public string cafeID, cafeName, cafeState, cafeCity, cafeStreetName, cafeZip;
}


//Later usage; this should be equivalent to your example:
var deserialisedJson = JsonConvert.DeserializeObject<LocationFromJson[][][][]>(json);

var locations = 
    deserialisedJson //4 levels of enumerable
        .SelectMany(o => o) //3 levels of enumerable
        .SelectMany(o => o) //2 levels of enumerable
        .SelectMany(o => o) //1 level of enumerable
        .Select(o => new Location {
            CafeId = o.First.cafeID,
            CafeName = o.First.cafeName,
            CafeState = o.First.cafeState,
            CafeCity = o.First.cafeCity,
            CafeStreetName = o.First.cafeStreetName,
            CafeZip = o.First.cafeZip,
        }).ToArray();

明确:这可能会或可能不会改变。您的示例不包含Location的类型声明,也不包含示例json,所以我在这里推测一点:Location.CafeId也可能是int;我无法从你的问题中说出来。

但这不应该离你需要的太远。