小编Abh*_*rma的帖子

如何在 C# (.NET Core 3.1) 中处理 POST 请求中的 JSON 数据

以前(在 .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)

.net c# json angular

8
推荐指数
1
解决办法
5107
查看次数

如何从 JSON 中提取其值为 true 的键

我有以下 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)

html json angular

6
推荐指数
1
解决办法
317
查看次数

如何根据下拉列表中的选定数字生成表单输入字段(选择)

我想根据下拉列表中的选定值生成输入文本框。

<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)

就在这个选择框之后,一些输入字段应该按照选定的数字出现。

html angular angular7

5
推荐指数
1
解决办法
583
查看次数

如何在 Angular 7 中从 JSON 生成 HTML 表单

我想从下面的 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)

html forms json angular angular7

5
推荐指数
1
解决办法
3599
查看次数

如何从打字稿中的JSON中提取特定值

我只想从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。请帮忙。

arrays typescript angular

1
推荐指数
3
解决办法
84
查看次数

标签 统计

angular ×5

html ×3

json ×3

angular7 ×2

.net ×1

arrays ×1

c# ×1

forms ×1

typescript ×1