xxxxxxxxxx
using Newtonsoft.Json;
// JSON string to be converted
string jsonString = "{\"name\":\"John\",\"age\":30}";
// Convert JSON string to object
var jsonObject = JsonConvert.DeserializeObject(jsonString);
// Access object properties
string name = jsonObject["name"].ToString();
int age = (int)jsonObject["age"];
// Print object properties
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
xxxxxxxxxx
using Newtonsoft.Json;
var jsonString = JsonConvert.SerializeObject(obj);
xxxxxxxxxx
var result = JsonConvert.DeserializeObject<YourClass>(jsonstring);
xxxxxxxxxx
using System;
using System.Text.Json;
// Define a class representing the structure of the JSON object
public class MyObject
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Program
{
public static void Main()
{
string jsonString = "{\"Id\": 1, \"Name\": \"John\"}";
// Deserialize the JSON string to an object of MyObject
MyObject obj = JsonSerializer.Deserialize<MyObject>(jsonString);
// Access and use the object properties
Console.WriteLine("Id: " + obj.Id);
Console.WriteLine("Name: " + obj.Name);
}
}
xxxxxxxxxx
using System;
using Newtonsoft.Json;
// Define a class to convert to JSON
public class MyClass
{
public string Name { get; set; }
public int Age { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
// Create an instance of MyClass
MyClass obj = new MyClass
{
Name = "John Doe",
Age = 30
};
// Convert object to JSON string
string json = JsonConvert.SerializeObject(obj);
Console.WriteLine(json);
}
}
xxxxxxxxxx
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
public class JsonToCSharpConverter
{
public static void Main()
{
string json = @"{
'id': 1,
'name': 'John Doe',
'age': 30,
'email': 'johndoe@example.com'
}";
JObject jsonObject = JObject.Parse(json);
string className = "MyClass";
string csharpCode = GenerateCSharpClass(jsonObject, className);
Console.WriteLine(csharpCode);
}
public static string GenerateCSharpClass(JToken jsonToken, string className)
{
if (jsonToken.Type == JTokenType.Object)
{
string classCode = $"public class {className}" + Environment.NewLine;
classCode += "{" + Environment.NewLine;
foreach (JProperty property in jsonToken)
{
string propertyName = property.Name;
string propertyType = GetCSharpType(property.Value.Type);
classCode += $" public {propertyType} {propertyName} {{ get; set; }}" + Environment.NewLine;
}
classCode += "}";
return classCode;
}
return null; // Or handle other data types as per requirement.
}
public static string GetCSharpType(JTokenType tokenType)
{
switch (tokenType)
{
case JTokenType.Boolean:
return "bool";
case JTokenType.Integer:
return "int";
case JTokenType.Float:
return "double";
case JTokenType.String:
return "string";
case JTokenType.Date:
return "DateTime";
case JTokenType.Guid:
return "Guid";
case JTokenType.Null:
case JTokenType.Undefined:
return "object";
default:
return "object"; // Or handle other data types as per requirement.
}
}
}
xxxxxxxxxx
{
"Class1":{
"id":4,
"user_id":"user_id_value",
"awesomeobject":{
"SomeProps1":1,
"SomeProps2":"test"
},
"created_at":"2015-06-02 23:33:90",
"updated_at":"2015-06-02 23:33:90",
"users":[
{
"id":"6",
"name":"Test Child 1",
"created_at":"2015-06-02 23:33:90",
"updated_at":"2015-06-02 23:33:90",
"email":"test@gmail.com"
},
{
"id":"6",
"name":"Test Child 1",
"created_at":"2015-06-02 23:33:90",
"updated_at":"2015-06-02 23:33:90",
"email":"test@gmail.com",
"testanadditionalfield":"tet"
} ]
},
"Class2":{
"SomePropertyOfClass2":"SomeValueOfClass2"
}
}
xxxxxxxxxx
public class Root{
public string schema {get; set;}
public string name {get; set;}
public string namespace1 {get; set;}
public string type {get; set;}
public Dictionary<String,String> fields{get; set;}
}