0
public void Set(string name, object content)
        {
   
         
            this.Json.Add(name, content);
            
        }

How can I convert content variable to Jtoken so that I can perform Json.add operation?

MikeCAT
  • 73,922
  • 11
  • 45
  • 70

1 Answers1

2

You can use JObject.FromObject:

this.Json.Add(name, JObject.FromObject(content));

Or JValue.FromObject if you are expecting values which are not mapped to json objects:

this.Json.Add(name, JValue.FromObject(content));
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 1
    @UbaidUllah cause boolean is not mapped to json object. You can use `JValue.FromObject(content)` if not only classes are expected. – Guru Stron Jul 22 '20 at 15:05
  • Solved , Thankyou – Ubaid Ullah Jul 22 '20 at 15:11
  • @UbaidUllah was glad to help. – Guru Stron Jul 22 '20 at 15:12
  • 4
    FYI, there is no [`JValue.FromObject()`](https://www.newtonsoft.com/json/help/html/Methods_T_Newtonsoft_Json_Linq_JValue.htm), you're actually calling the static method on the base class `JToken`. See: [JSON.NET: Why Use JToken--ever?](https://stackoverflow.com/a/38212978/3744182). – dbc Jul 22 '20 at 15:19