我有一个JSON数据源,我试图模拟绑定。问题是属性名称是对象的id。例如:
{
"1": someObject,
"2": someObject,
"3": someObject
}
我用来检索和解析它的代码是:
WebRequest req = WebRequest.Create(UnitURL);
WebResponse resp = req.GetResponseAsync().Result;
string json;
using(var s = new StreamReader(resp.GetResponseStream()))
{
json = s.ReadToEndAsync().Result;
}
JsonConvert.DeserializeObject<rootObject>(json);
我希望rootObject看起来像:
public class rootObject
{
public Dictionary<int, someObject> Things { get; set; }
}
答案 0 :(得分:1)
然后需要更新Json以匹配所需的对象图,例如......
{
"Things": {
"1": someObject,
"2": someObject,
"3": someObject
}
}
或者将原始JSON反序列化为Dictionary<int, someObject>
,然后将其分配给rootObject
,例如......
var things = JsonConvert.DeserializeObject<Dictionary<int, someObject>>(json);
var root = new rootObject {
Things = things
};