Abh*_*rma 8 .net c# json angular
以前(在 .Net Core 2.1 中)我使用以下方法成功处理了 JSON 数据
[HttpPost]
[Route("sendJsonData")]
public JObject saveTemplate(JObject jsonString)
{
string templateName = (string)jsonString.SelectToken("templateName");
string filePathAndName = "D:\\" + "templates\\" + templateName + ".txt";
using (StreamWriter file = File.CreateText(@filePathAndName))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, jsonString);
}
return jsonString;
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用 .Net Core 3.1 创建相同的方法时。它无法正常工作,显示 JObject 出现一些错误。我从下面的代码中得到那个 JSON
onSubmit() {
this.http.post("https://localhost:44350/ReportAPI/sendJsonData", this.surveyForm.value)
.subscribe(
data => console.log("success!", data),
error => console.error("couldn't post because", error)
);
}
Run Code Online (Sandbox Code Playgroud)
以下是错误(来自邮递员的回复)
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|6e209814-4dd7396cc2dfa182.",
"errors": {
"$": [
"The JSON value could not be converted to System.Collections.Generic.IEnumerable`1[Newtonsoft.Json.Linq.JToken]. Path: $ | LineNumber: 0 | BytePositionInLine: 14."
]
}
}
Run Code Online (Sandbox Code Playgroud)
JSON
{
"templateName":"BFS Survey",
"surveyQuestions":[
{
"questionTitle":"Name",
"questionType":"Text",
"questionGroup":{
}
},
{
"questionTitle":"Age",
"questionType":"Number",
"questionGroup":{
}
},
{
"questionTitle":"Gender",
"questionType":"Single choice",
"questionGroup":{
"options":[
{
"optionText":"Male"
},
{
"optionText":"Female"
},
{
"optionText":"Other"
}
],
"showRemarksBox":false
}
},
{
"questionTitle":"Skills",
"questionType":"Multi choice",
"questionGroup":{
"options":[
{
"optionText":"Java"
},
{
"optionText":"Angular"
},
{
"optionText":"Python"
},
{
"optionText":"R"
}
],
"showRemarksBox":false
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
代码使用 .Net Core 2.1 完美运行,但不适用于 3.1。请建议我如何解决这个问题。
| 归档时间: |
|
| 查看次数: |
5107 次 |
| 最近记录: |