[英]How to de-serialize JSON array in c#
Hi I am developing web application in c#. 嗨,我正在用C#开发Web应用程序。 I have one JSON object. 我有一个JSON对象。 I am trying to de-serialize the object as below. 我正在尝试反序列化对象,如下所示。 Below is my result. 以下是我的结果。
[ {
"id": "ce053195-ae48-4457-bb88-6ca32f236bd1",
"model": {
"objectId": [
"c760d95e-3fcb-43d1-a7db-111d3384ecbc"
],
"name": [
"Device1-Configuration"
]
},
"childs": 0,
"access": 0,
"allow": 0
},
{
"id": "42ad8eb6-9f35-447c-8ea0-e1346dee410c",
"model": {
"objectId": [
"c760d95e-3fcb-43d1-a7db-111d3384ecbc"
],
"name": [
"Device1-DeviceModel"
]
},
"childs": 1,
"access": 0,
"allow": 0
}
]
Below are my models. 下面是我的模型。
public partial class Resource
{
public System.Guid Id { get; set; }
public Models Model { get; set; }
public bool Selected { get; set; }
public bool hasChildren { get; set; }
public bool IsAllowed { get; set; }
}
Below is my model class. 下面是我的模型课。
public partial class Models
{
public List<string> Name { get; set; }
}
I am trying to assign name array from the models. 我正在尝试从模型分配名称数组。
foreach (JObject singScopeRes in result)
{
var permission = new Rbac.BusinessEntities.V2.Resource();
permission.Id = new Guid(singScopeRes["id"].ToString());
permission.Model = new Rbac.BusinessEntities.V2.Models()
{
Name= singScopeRes["model"]["name"][0]
};
}
This is throwing error saying: 这是抛出错误的说法:
cannot implicit convert type newtonsoft.json.linq.jtoken to system.generic.list 无法将类型newtonsoft.json.linq.jtoken隐式转换为system.generic.list
Can someone help me to fix this issue? 有人可以帮我解决此问题吗?
Use a tool like json2csharp to generate your model: 使用json2csharp之类的工具来生成模型:
public class Model
{
public List<string> objectId { get; set; }
public List<string> name { get; set; }
}
public class Resource
{
public string id { get; set; }
public Model model { get; set; }
public int childs { get; set; }
public int access { get; set; }
public int allow { get; set; }
}
Now you can deserialize your json like this: 现在,您可以像这样反序列化您的json:
var obj = JsonConvert.DeserializeObject<Resource[]>(json);
If you want to stick with your current code you can do so by casting the JToken
while assigning it to the Name
property: 如果要坚持使用当前代码,可以通过在将JToken
分配给Name
属性的同时铸造JToken
来实现:
foreach (JObject singScopeRes in result)
{
var permission = new Rbac.BusinessEntities.V2.Resource();
permission.Id = new Guid(singScopeRes["id"].ToString());
permission.Model = new Rbac.BusinessEntities.V2.Models()
{
Name = (singScopeRes["model"]["name"]).ToObject<List<string>>()
};
}
You could try to Convert your JSON to an dynamic
and then loop over those values as following: dynamic obj = JsonConvert.DeserializeObject(JSON);
您可以尝试将JSON转换为dynamic
值,然后如下遍历这些值: dynamic obj = JsonConvert.DeserializeObject(JSON);
Another option is to make your Objects have the same structure as the JSON. 另一个选择是使您的对象具有与JSON相同的结构。
public class Model
{
public List<string> objectId { get; set; }
public List<string> name { get; set; }
}
public class Resource
{
public string id { get; set; }
public Model model { get; set; }
public int childs { get; set; }
public int access { get; set; }
public int allow { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.