Good day, I need to assign variables to JObject
:
JObject json = JObject.Parse(@"{
RequestData: {
AccountCredential: {
AccountLink: 'dFyRCVYNM0aGb1LSR/0B8e+eSVr1Zf1xj9YHmMVoiZWk28XtWEyIxEbGXnr1EdvS+QBPzjweau7tbf5QlDv97IQ2jwfUB=='
},
AccountNumber: '000011887755',
BookingDateStart: '2022-07',
BookingDateEnd: '2022-07'
} }");
I want to pass values to AccountLink
, AccountNumber
, BookingDateStart
, BookingDateEnd
. If you notice, I set the values to hard, but these change, where I should use it by passing the parameters.
I have tried to use the JsonSerializerOptions
, but I get an error
Could not load file or assembly 'System.Text.Json'
I want to be able to use it in an asynchronous task.
Does anyone have any ideas? Your help is appreciated.
There are two major C# JSON libraries: Newtonsoft.Json
and System.Text.Json
. JObject
is a class from the former, and JsonSerializerOptions
is a class from the latter. You can't just mix them together. If you want to modify Newtonsoft.Json serialization, you need to look up how to do that in Netwonsoft.Json.
That having been said, you don't need a serializer for what you are trying to do. Creating a JObject
from variable data is simple in Netwonsoft.Json. Your code would look similar to that (fiddle):
var json = new JObject(
new JProperty("RequestData",
new JObject(
new JProperty("AccountCredential",
new JObject(
new JProperty("AccountLink", "...")
)
),
new JProperty("AccountNumber", "..."),
new JProperty("BookingDateStart", "..."),
new JProperty("BookingDateEnd", "...")
)
)
);
Just replace "..." with the value you want to fill your JObject with.