以前(在 .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 …Run Code Online (Sandbox Code Playgroud) 我有以下 JSON,我想从中提取技能是真的。
[
{
"_id":"5de9f351baca28556c6a4b71",
"Name":"Harsha",
"Age":20,
"Gender":"M",
"Skills":{
"Java":"",
"Mule":true,
"Angular":""
}
},
{
"_id":"5de9f358baca28556c6a4b72",
"Name":"Anji",
"Age":21,
"Gender":"M",
"Skills":{
"Java":"",
"Mule":true,
"Angular":true
}
},
{
"_id":"5dea110297c2b65298b136e4",
"Name":"Abhi",
"Age":25,
"Gender":"M",
"Skills":{
"Java":"",
"Mule":true,
"Angular":""
}
}
]
Run Code Online (Sandbox Code Playgroud)
我可以使用以下代码打印其余数据
<table *ngIf="formTemplate">
<tr>
<th *ngFor="let header of questionTitleArray" >{{header}}</th>
</tr>
<tr *ngFor="let data of surveyDataFromDB">
<ng-container *ngFor="let head of questionTitleArray">
<td>{{data[head]}}</td>
</ng-container>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
(这里的 JSON 是“surveyDataFromDB”)
下面是我得到的输出
Name Age Gender Skills
Harsha 20 M [object Object]
Anji 21 M [object Object] …Run Code Online (Sandbox Code Playgroud) 我想根据下拉列表中的选定值生成输入文本框。
<mat-label>Options</mat-label>
<mat-form-field>
<select matNativeControl name="Options" required>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
就在这个选择框之后,一些输入字段应该按照选定的数字出现。
我想从下面的 JSON 生成一个 HTML 表单。
{
"templateName":"C-Learn Survey",
"surveyQuestions":[
{
"questionTitle":"Enter your name",
"questionType":"Text",
"questionGroup":{
}
},
{
"questionTitle":"Enter your empid:",
"questionType":"Text",
"questionGroup":{
}
},
{
"questionTitle":"Select your technologies",
"questionType":"Multi choice",
"questionGroup":{
"options":[
{
"optionText":"Java"
},
{
"optionText":"Mule"
},
{
"optionText":".net"
}
],
"showRemarksBox":false
}
},
{
"questionTitle":"Gender",
"questionType":"Single choice",
"questionGroup":{
"options":[
{
"optionText":"Male"
},
{
"optionText":"Female"
}
],
"showRemarksBox":false
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
例如
{
"questionTitle":"Enter your name",
"questionType":"Text",
"questionGroup":{
}
Run Code Online (Sandbox Code Playgroud)
对于上面的 json 应该有一个像这样的 html 表单元素
<label>Enter your name</label> …Run Code Online (Sandbox Code Playgroud) 我只想从json下面提取“ phaseValue”部分,并将其存储到数组中。
{
"templateName":"TemplateExample",
"phaseExecutions":{
"PRE":[
{
"phaseType":"text",
"phaseValue":"enter your name"
},
{
"phaseType":"number",
"phaseValue":"enter your mobile number"
},
{
"phaseType":"email",
"phaseValue":"enter your email"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我从下面的代码获取以上数据
getTemplateByName():any
{
this.httpService.get('http://localhost:57611/Api/Employee/GetTemplateByName/'+this.selectedTemplate).subscribe(
data => {
this.templateInJsonFormat = data;
this.getTemplateFromSubscribe(this.templateInJsonFormat);
}
);
}
getTemplateFromSubscribe(temp:any)
{
this.finalTemplateFromSubscribe = temp;
console.log(this.finalTemplateFromSubscribe);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用打字稿3.5.3。请帮忙。