Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass string variables to JObject C# – Could not load file or assembly 'System.Text.Json

Good day, I need to assign variables to JObjet:

 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 error Could not load file or assembly ‘System.Text.Json. I want to be able to use it in an asynchronous task.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If anyone has any ideas, your help is appreciated.

Greetings

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading