[
{
"canEdit": false,
"canView": false,
"createTime": "2016-10-31 09:32:08",
"creatorId": "7962",
"creatorName": "xx",
"currentNodeId": "24301",
"workflowBaseInfo": {
"workflowId": "2597",
"workflowName": "xxx",
"workflowTypeId": "65",
"workflowTypeName": "帐号与权限类"
}
},
{
"canEdit": false,
"canView": false,
"createTime": "2016-08-22 11:50:49",
"creatorId": "13163",
"creatorName": "钟少英",
"currentNodeId": "12420",
"workflowBaseInfo": {
"workflowId": "1425",
"workflowName": "员工调动办理流程",
"workflowTypeId": "35",
"workflowTypeName": "人力资源类"
}
]
返回的json序列化后,创建实体类后,怎么获取workflowBaseInfo里的值???
不知道你指的是哪方面的?
foreach (oatest c in fsWork)//List的元素
{
foreach (WorkflowBaseInfo s in ??)
{
Response.Write(s.workflowName + "</br>");
}
这里需要怎么处理?
@XianBin_Zhang:表示还是没看懂你的语法,不知道你是不是js写法。但我觉的你的问题js解决很简单,就循环一下就得到了。
示例如下:
var data = [
{
"canEdit": false,
"canView": false,
"createTime": "2016-10-31 09:32:08",
"creatorId": "7962",
"creatorName": "xx",
"currentNodeId": "24301",
"workflowBaseInfo": {
"workflowId": "2597",
"workflowName": "xxx",
"workflowTypeId": "65",
"workflowTypeName": "帐号与权限类"
}
},
{
"canEdit": false,
"canView": false,
"createTime": "2016-08-22 11:50:49",
"creatorId": "13163",
"creatorName": "钟少英",
"currentNodeId": "12420",
"workflowBaseInfo": {
"workflowId": "1425",
"workflowName": "员工调动办理流程",
"workflowTypeId": "35",
"workflowTypeName": "人力资源类"
}
}
];
data.forEach(function (e) {
console.log(e.workflowBaseInfo.workflowName)
})
workflowBaseInfo也是一个对象啊。
class X
{
....
public workflowBaseInfo info{get;set;}
}
instanceOfX.info.workflowId
....
现在是获取不了workflowBaseInfo 里的值
json数据少了一个}-------"workflowTypeName": "人力资源类"
}这里要追加一个}
帮你在写demo 稍等
恩,
是我这里少写了一个
@XianBin_Zhang:
C# Code:
1 string jsonStr = "[{\"id\":\"815bb899-8d70-4745-b799-7e68840a2b34\",\"UserInfo\":{\"Name\":\"李四\",\"Age\":23}},{\"id\":\"815bb899-8d70-4745-b799-7e68840a2b34\",\"UserInfo\":{\"Name\":\"王五\",\"Age\":25}}]"; 2 List<User> userlist = JsonToList<User>(jsonStr); 3 4 // 遍历list获取UserInfo对象的任意属性。 5 6 7 public List<T> JsonToList<T>(string jsonStr) 8 {
if(jsonStr==null)return default(T); 9 JavaScriptSerializer js = new JavaScriptSerializer(); 10 List<T> t = (List<T>)js.Deserialize(jsonStr, typeof(List<T>)); 11 12 return t; 13 14 } 15 16 17 public class User 18 { 19 public string Id { get; set;} 20 21 public UserInfo UserInfo { get; set; } 22 23 } 24 25 public class UserInfo 26 { 27 public string Name { get; set; } 28 29 public int Age { get; set; } 30 }
//仅供参考
@~扎克伯格: 现在的问题是遍历取不到Name,Age的值
@XianBin_Zhang:
1 foreach (User user1 in userlist) 2 { 3 User user = new User(); 4 user.UserInfo.Name = user1.UserInfo.Name; 5 user.UserInfo.Age = user1.UserInfo.Age; 6 }
@~扎克伯格: List<User> userlist = JsonToList<User>(jsonStr); JsonToList<User>(jsonStr)这是那里来的? public T JsonToEntity<T>(string jsonStr)这个吗? List<MyWorkflow> userlist = JsonToEntity<MyWorkflow>(item); 这样无法转换
@~扎克伯格: 知道什么问题了,感谢!!!
@XianBin_Zhang: 已修改