【发布时间】:2013-03-18 16:20:32
【问题描述】:
我正在尝试在 ASP.NET 服务器上转换嵌套的 JSON 对象。传入的 JSON 字符串看起来像这样 -
data: {
user_id: 1,
taskid: "1234",
list: {
"item-1": { one: 1, two: 2 },
"item-2": { one: 1, two: 2 }
//.. where number of items is unknown
}
}
我曾尝试以这种方式使用JSON.Decode 解码数据
public class Data {
public int user_id { get; set; }
public string taskid { get; set; }
public List<object> list { get; set; }
}
public class DataList {
List<Data> data { get; set; }
}
// if isPost etc..
var decodedData = JSON.Decode<DataList>(Request["data"])
但是当我尝试迭代 decodedData 时,我得到了一个错误 -
foreach 语句不能对类型变量进行操作 'ASP._Page_that_cshtml.DataList' 因为 'ASP._Page_that_cshtml.DataList' 不包含公共定义 对于'GetEnumerator'
当我尝试以这种方式将 decodedData 转换为 List 时 -
List<Data> decodedData = JSON.Decode<DataList>(Request["data"])
我又抛出一个错误
CS0030:无法将类型“
ASP._Page_that_cshtml.DataList”转换为“System.Collections.Generic.List<ASP._Page_that_cshtml.DataList>”
您能否建议一种适当的方法将嵌套的 JSON 对象转换为 C# 对象并对其进行迭代?
PS:故意省略尾随分号
【问题讨论】:
标签: c# javascript asp.net json