【发布时间】:2018-07-09 03:20:26
【问题描述】:
早安,
我需要帮助来提取 JSON 字符串并将其提供给我的视图模型。
在我的示例中,我有一个具有 2 个类的 ViewModel。
类:
public class Student {
public int StudentId {get;set;}
public string Firstname {get;set;}
public string Lastname {get;set;}
}
public class Address {
public int AddressId {get;set;}
public string Street {get;set;}
}
视图模型:
public class StudentAddressViewModel{
public Student Student {get;set;}
public Address Address {get;set}
}
控制器:
public async Task<IActionResult> Create(IFormCollection studentInfo){
// wherein studentInfo is the key
...
}
在我的控制器中,我正在发送这个 JSON 字符串。
{[studentInfo,{"Student":{"firstname":"Johhny","lastname":"Bravo"},"Address":{"street":"New york street..."}])
我正在尝试这个:
var studentInfo = studentInfo["studentInfo"];
var value = JsonConvert.DeserializeObject<Dictionary<string,string>>(studentInfo);
var studentVm = new StudentAddressViewModel{
new Student{
Firstname: value["firstname"], Lastname: value["lastname"]
},
new Address{
Address: value["address"]
}
}
但我的 value 为空。
有什么帮助吗?
【问题讨论】:
-
您必须提供更多信息,例如您在控制器端编写的返回该 Json 的内容以及您提供示例代码的位置。
-
您拥有的 JSON 无效。另外,您所说的 webview 是什么意思,您将其发送到哪里?
-
你得到的是这种格式的json吗? {[studentInfo,{"Student":{"firstname":"Johhny","lastname":"Bravo"},"Address":{"street":"New york street..."}])
-
是的@NoorAllSafaet
-
开头的
{与 JSON 中的结尾)不匹配。这不是问题吗?另外,studentInfo如何是 JSON 中的有效数组元素?它不是 Javascript 原语,也不是字符串。似乎是一个很大的错误。最后,如果{开头是一个对象,那么键在哪里,值在哪里?我知道这可能不是一直到外面的真正的 JSON 对象,但你说它是。否则,您的问题根本没有足够的信息来回答。这个 JSON 字符串来自哪里?studentInfo = studentInfo["studentInfo"];到底是什么?
标签: c# asp.net json asp.net-core json.net