特定 Json 的问题,无法转换
problems with a particular Json, cant Convert
有没有办法只获取 json 的一部分?,我需要 (Entity) 然后将它发送到视图,我尝试过以不同的方式转换它但没有成功,请帮助.
JSON - 我不需要 Statuscode 和 Message,Entity 才是真正的数据
{"StatusCode":200,"Message":"succesfull list.","Entity":"[{\"Name\":\"name 1\",\"Enable\":1,\"Celphone\":null},{\"Name\":\"name 2\",\"Enable\":1,\"Celphone\":\"0\"}]"}
型号
public class Root
{
public int StatusCode { get; set; }
public string Message { get; set; }
public string Entity { get; set; }
}
控制器
var clientsEntity= JsonConvert.DeserializeObject<Root>(response);
return View(clientsEntity);
错误:
The model element passed to the dictionary is of type 'APP.Models.Root', pbut this dictionary requires a model element of type 'System.Collections.Generic.IEnumerable`1[APP.Models.EntityModel]'.
我什至尝试创建一个额外的 class
public class Entity
{
public string Name { get; set; }
public bool Enable { get; set; }
public string Celphone { get; set; }
}
var data = JsonConvert.DeserializeObject <IEnumerable<IDictionary<string, Object>>>(clientsEntity.Entity); //data has data needed
returnView(data);
错误:
The model element passed to the dictionary is of type'System.Collections.Generic.List1[System.Collections.Generic.IDictionary
2[System.String,System.Object]]', but this dictionary requires a model element of type 'System.Collections.Generic.IEnumerable`1[APP.Models.EntityModel]'.
我尝试了很多方法,但我卡住了,没有想法
您的 JSON 是双重序列化的。要处理它,您首先需要将 JSON 反序列化为 Root
class,然后将 Entity
属性 反序列化为 List<Entity>
.
var root = JsonConvert.DeserializeObject<Root>(json);
var entities = JsonConvert.DeserializeObject<List<Entity>>(root.Entity);
这是一个工作示例:https://dotnetfiddle.net/jjbFNc
您已正确确定您的 json 是双重序列化的,您需要对其进行反序列化 Entity
属性 第二次,但您在该尝试中使用了错误的类型。
你遇到的错误意味着视图需要 IEnumerable<APP.Models.EntityModel>
类型的模型(通过视图顶部的 @model IEnumerable<APP.Models.EntityModel>
之类的东西声明),但你实际上是在向它传递一个模型不同类型(第一次尝试 Root
,第二次尝试 IEnumerable
字典)。因此,您需要更改视图期望的模型或反序列化以正确类型。对于第二个,您可以尝试下一个:
var clientsEntity= JsonConvert.DeserializeObject<Root>(response);
var data = JsonConvert.DeserializeObject<IEnumerable<APP.Models.EntityModel>>(clientsEntity.Entity);
returnView(data);
有没有办法只获取 json 的一部分?,我需要 (Entity) 然后将它发送到视图,我尝试过以不同的方式转换它但没有成功,请帮助.
JSON - 我不需要 Statuscode 和 Message,Entity 才是真正的数据
{"StatusCode":200,"Message":"succesfull list.","Entity":"[{\"Name\":\"name 1\",\"Enable\":1,\"Celphone\":null},{\"Name\":\"name 2\",\"Enable\":1,\"Celphone\":\"0\"}]"}
型号
public class Root
{
public int StatusCode { get; set; }
public string Message { get; set; }
public string Entity { get; set; }
}
控制器
var clientsEntity= JsonConvert.DeserializeObject<Root>(response);
return View(clientsEntity);
错误:
The model element passed to the dictionary is of type 'APP.Models.Root', pbut this dictionary requires a model element of type 'System.Collections.Generic.IEnumerable`1[APP.Models.EntityModel]'.
我什至尝试创建一个额外的 class
public class Entity
{
public string Name { get; set; }
public bool Enable { get; set; }
public string Celphone { get; set; }
}
var data = JsonConvert.DeserializeObject <IEnumerable<IDictionary<string, Object>>>(clientsEntity.Entity); //data has data needed
returnView(data);
错误:
The model element passed to the dictionary is of type'System.Collections.Generic.List
1[System.Collections.Generic.IDictionary
2[System.String,System.Object]]', but this dictionary requires a model element of type 'System.Collections.Generic.IEnumerable`1[APP.Models.EntityModel]'.
我尝试了很多方法,但我卡住了,没有想法
您的 JSON 是双重序列化的。要处理它,您首先需要将 JSON 反序列化为 Root
class,然后将 Entity
属性 反序列化为 List<Entity>
.
var root = JsonConvert.DeserializeObject<Root>(json);
var entities = JsonConvert.DeserializeObject<List<Entity>>(root.Entity);
这是一个工作示例:https://dotnetfiddle.net/jjbFNc
您已正确确定您的 json 是双重序列化的,您需要对其进行反序列化 Entity
属性 第二次,但您在该尝试中使用了错误的类型。
你遇到的错误意味着视图需要 IEnumerable<APP.Models.EntityModel>
类型的模型(通过视图顶部的 @model IEnumerable<APP.Models.EntityModel>
之类的东西声明),但你实际上是在向它传递一个模型不同类型(第一次尝试 Root
,第二次尝试 IEnumerable
字典)。因此,您需要更改视图期望的模型或反序列化以正确类型。对于第二个,您可以尝试下一个:
var clientsEntity= JsonConvert.DeserializeObject<Root>(response);
var data = JsonConvert.DeserializeObject<IEnumerable<APP.Models.EntityModel>>(clientsEntity.Entity);
returnView(data);