简体   繁体   中英

How to store Json Body of post request in C#

Need help in adding Json body in C3 code where I can use my own variable in json body.

Below is Json Body of an API.

{
"allOrNone" : false,
"records" : [{
"attributes" : {"type" : "Case"},
"Id" : "12345",
"Comment__c": "testing comment "
}]
}

In C# code, I want put my variable in place of id and comment while sending request. I have tried using code that is generated by postman but I am not able to change those parameter.

Code generated by postman

图像

The simplest way is to deserialize and then serialize again.

 public class MyObject 
 {
  public int ExistingPropertyOnJson { get; set;}
  public string NonExistingPropertyOnJson { get; set; }
 }

var deserializedObject = JsonConvert.DeserializeObject<MyObject>(YourResponseJson);

deserializedObject.NonExistingPropertyOnJson  = "MyValue";

var newJsonBody = JsonConvert.SerializeObject(deserializedObject);

Postman's code is essentially irrelevant. Nobody constructs JSON requests by concatenating strings. They use a class with the necessary properties, set the values, serialize the class to JSON and then send POST that payload. With many libraries serialization is handled by the library itself.

Visual Studio and various online services can generate classes from a JSON sample. In this case though, the classes are simple

The request can be represented by classes like these one :


public class MyRecord
{
    public string Id {get;set;}
    public string Comment__c {get;set;}

    public Dictionary<string,string> Attributes {get;set;}
}


public class MyDataObject
{
    public bool AllOrNone {get;set;}
    public MyRecord[] Records {get;set;}

}

Using NET's HttpClient and the System.Net.Http.Json package all you need to do is construct a new instance of the object, fill it with the data you want:

var myObject=new MyDataObject {
    AllOrNone=false,
    Records=new []{
                Id="12345",
                Attributes = new Dictionary<string,string>{
                               ["type"]="Case"
                           },
                Comment__c ="testing comment "
            }
};

And then call PostAsJsonAsync

await client.PostAsJsonAsync(url,myObject);

If you want to modify an API's response and send it back :

var myObject=await client.GetAsJsonAsync(url1);
myObject.AllOrNone=false;
await client.PostAsJsonAsync(url2,myObject);

System.Net.Http.Json is included in .NET (Core) 5 and later, and available for previous .NET Core and .NET Framework versions through the System.Net.Http.Json package.

GetAsJsonAsync and PostAsJsonAsync serialize JSON text to objects and vice versa using .NET's System.Text.Json. If you want to use a different serializer, eg JSON.NET, you need to retrieve the response as a string or stream and deserialize it.

var json = await client.GetAsStringAsync(url1);
var myObject = JsonConvert.DeserializeObject<MyDataObject>(json);

To post the object, you need to construct a StringContent object with the serialized JSON string and the correct content type:

var json=JsonConvert.SerializeObject(myObject);
var content=new StringContent(json,Encoding.UTF8,"application/json");
await client.PostAsync(url,content);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM