I have few different JSON objects.
Fist JSON object
{
"api": {
"NAME":"CUSTOMER_CREATE",
"QUERY":"CUSTOMER_CREATE"
},
"header" : {
"PartyNumber": "1488002",
"OrganizationName": "Test Organization-02",
"RawPhoneNumber": "9199199199",
"PartyUsageCode": "EXTERNAL_LEGAL_ENTITY",
"Address": [
{
"AddressType": "BILL_TO",
"Address1": "77 College Rd",
"Address2": "London NW10 5SE",
"Address3": "London NW10 5SF",
"Address4": "London NW10 5SG",
"City": "London",
"Country": "GB",
"PostalCode": "NW10 5ES"
}
Second JSON object
"api": {
"NAME":"ITEM_CREATE",
"QUERY":"ITEM_CREATE"
},
"header" :
{
"OrganizationCode": "IM_MASTER_ORG",
"ItemClass" : "Root Item Class",
"ItemNumber" : "TEST-01",
"ItemDescription" : "TEST-01",
"ItemStatusValue" : "Active",
"PrimaryUOMValue" : "Each",
"LifecyclePhaseValue" : "Production"
}
Also I have many JSON objects like this. All JSON object are not same. So I want to write One C# class for convert this any JSON object to C# Object. So can you please me one class to convert these JSON object.
Currently I am using this class to convert Json object. but I want to make this as a dynamic class
public class MHScaleMessage {
//public string api { get; set;}
public Dictionary<string, string> api { get; set;} = new Dictionary<string, string>();
public Dictionary<string, string> header {get; set; } = new Dictionary<string, string>();
public List<Dictionary<string, string>> lineItems { get; set; } = new List<Dictionary<string, string>>();
public static string GetValue(Dictionary<string, string> d, string key, string defaultVal="") {
if (key == null) return defaultVal;
return d.TryGetValue(key, out string val) ? val : defaultVal;
}
public static int GetValueAsInt(Dictionary<string, string> d, string key, int defaultVal=0) {
if (key == null) return defaultVal;
string val = d.TryGetValue(key, out val) ? val : null;
if (val == null) return defaultVal;
return int.TryParse(val, out _) ? 0 : defaultVal;
}
public string ToJsonString() {
var options = new JsonSerializerOptions
{
WriteIndented = true,
IgnoreNullValues = true
};
return System.Text.Encoding.UTF8.GetString(JsonSerializer.SerializeToUtf8Bytes(this, options));
}
public string _ToString() {
string s="";
s = "{"; s = "\n";
s = "api = " api; s = ","; s = "\n";
s = "header ="; s = "\n";
s = "{";
header.ToList().ForEach(x => s = x.Key " = " x.Value "\n");
s = "}"; s = ","; s = "\n";
s = "lineItems"; s = "\n";
s = "{"; s = "[";
lineItems.ForEach(x => {
x.ToList().ForEach(y => {
s = y.Key " = " y.Value;
s = ","; s = "\n";
}
);
});
s = "]"; s = "}"; s = "\n";
s = "}";
return s;
}
}
After that I am using following method to perform API. to this method I can pass parameter.
private static MHScaleMessage PerformAPIFunction(MHScaleMessage requestObj)
{}
CodePudding user response:
There are multiple ways to convert a JSON object to a C# sharp object and here I am going to show a few ways.
One Line Code
var oMycustomclassname = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(jsonString);
If you have your own class then change var
to your own class.
Use JSON.NET
There is a recommended way to convert JSON to C# object and that is JSON.NET Have a look at the github repo to get more information. Here is an example for your case.
public class User
{
public User(string json)
{
JObject jObject = JObject.Parse(json);
JToken jUser = jObject["api"];
name = (string) jUser["NAME"];
query = (string) jUser["QUERY"];
JToken jUser1 = jObject["header"];
OrganizationCode = (string) jUser1["OrganizationCode"];
ItemClass = (string) jUser1["ItemClass"];
ItemNumber = jUser1["ItemNumber"];
}
public string api_Name{ get; set; }
public string api_Query { get; set; }
public string header_OrganizationCode{ get; set; }
public string header_ItemClass{ get; set; }
}
// Use
private void Run()
{
string json = @"{"api": {
"NAME":"ITEM_CREATE",
"QUERY":"ITEM_CREATE"
},
"header" :
{
"OrganizationCode": "IM_MASTER_ORG",
"ItemClass" : "Root Item Class",
"ItemNumber" : "TEST-01",
"ItemDescription" : "TEST-01",
"ItemStatusValue" : "Active",
"PrimaryUOMValue" : "Each",
"LifecyclePhaseValue" : "Production"
}";
User user = new User(json);
Console.WriteLine("Name : " user.name);
Console.WriteLine("Teamname : " user.teamname);
Console.WriteLine("Email : " user.email);
Console.WriteLine("Players:");
foreach (var player in user.players)
Console.WriteLine(player);
}
CodePudding user response:
You can get your requirement easily by using Newtonsoft.Json library. I am writing down the one example below have a look into it.
Class for the type of object you receive:
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
}
Code:
static void Main(string[] args)
{
string json = "{\"ID\": 1, \"Name\": \"Abdullah\"}";
Student student = JsonConvert.DeserializeObject<User>(json);
Console.ReadKey();
}
Simple way to parse your json.