create c# class from json code example

Example 1: how to create object in php wihtout class

//You can use stdClass which is sort of empty template   
$object = new stdClass();

//Assign property
$object->property = 'Here we go';

//Now if we dump it
 var_dump($object);
   /*
   outputs:
   object(stdClass)#2 (1) {
      ["property"]=>
      string(10) "Here we go"
    }
   */
//Even shorter
$object = (object) ['property' => 'Here we go'];

Example 2: json to csharp

//Make use of the third party library called Newton soft.
  
//To serialize a JSON object to C# class. The below will help. 
//Note: The C# class and JSON Object should have the same structure
  public static T Deserialize(string serializedData)
        {
            var deJson = JsonConvert.DeserializeObject<T>(serializedData);
            return deJson;
        }

//To de-serialize a JSON object to C# class. The below will help. 
//Note: The C# class and JSON Object should have the same structure
//The contract resolver and settings are only needed if you have specific attributes in your JSON string
 public static string Serialize(object objectName)
        {
            var settings = new JsonSerializerSettings()
            {
                ContractResolver = new OrderedContractResolver()
            };
            var json = JsonConvert.SerializeObject(objectName, Formatting.Indented, settings);
            return json;

        }
//No settings:
 public static string Serialize(object objectName)
        {
            var json = JsonConvert.SerializeObject(objectName, Formatting.Indented);
            return json;

        }

Example 3: javascript create class

class Car {
  constructor(brand) {
    this.carname = brand;
  }
}

Example 4: c# create a json object

dynamic ret = new JObject();

ret = new JObject(new JProperty("vendorDetails", new JObject()));

ret.vendorDetails.Add(new JProperty("vendorName", "Vendor Name"));

Tags:

Php Example