如何通过客户端 .net core windows 应用程序更新 json 特定键值

How to update json specific key value through client side .net core windows application

我在 .net core 5.x 中有一个 Restful Web 服务,其中 returns JSON 来自 Postgres 的数据,我想更新 [=] 的特定键值17=] 通过客户端(windows 应用程序),我也想反映在 Postgres DB 上。 我还在客户端使用 .netcore 3.1.3。

````C#`````
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Text.Json;
using HttpClientTest_Console.Models;
using Newtonsoft.Json;

namespace HttpClientTest_Console
{
    class Program {
    static readonly HttpClient client = new HttpClient();
    static async Task Main(string[] args){
    try {
  HttpResponseMessage response = await client
 .GetAsync("/api/Employee/GetEmployee");
  response.EnsureSuccessStatusCode();
  string responseBody = await response.Content.ReadAsStringAsync();
  Console.WriteLine(responseBody);
  }
catch (HttpRequestException e)
{
  Console.WriteLine("\nException Caught!");
  Console.WriteLine("\nMessage : {0}",e.Message);
}
}}}

会是这样的。下面的代码假定您在像“/api/Employee”这样的服务器上有一个数据更新 api 和一些本地 class 将数据反序列化为。

string responseBody = await response.Content.ReadAsStringAsync();

var data = JsonConvert.Deserialize<SomeObj>(responseBody);

data.Field = "New Data";

await client.PostAsync("/api/Employee", JsonConvert.Serialize(data));