I have object in my app in c# i need to serialize it to JSON but I need to custom the result json to be like below :
My class :
public class OrderStatus
{
public string title { get; set; }
public int statusNo { get; set; }
public string description { get; set; }
public string message { get; set; }
public string icon { get; set; }
}
I need to convert it to
{
"1": {
"icon": "orange_warning",
"title": "Pending Processing",
"message":
"We are processing your payment on our end which could take up to 30 minutes",
"description":
"Feel free to continue shopping while we process your payment. You can check the status of your order in Order History at any time."
},
"2": {
"icon": "done_success",
"title": "Order Successfully Placed",
"message": "",
"description":
"We have sent an email to your email address confirming your order."
}
}
the number is shown in json is StatusNo in my class I use this method to serialize the class
new JavaScriptSerializer().Serialize(model)
2
Answers
the structure you want to convert from that class is not have harmony.
the good way to convert that is change your structure.
for that you can use Json2csharp.com. copy your json structure and past into that.
Thanks to my mental powers, I would guess, that you have a list of
OrderStatus
and that the key in your resulting json is thestatusNo
property of your C# class.To get a specific structure as JSON you should always create special classes that match the structure and convert between your classes and at the end use the default JSON serialization process. In your case it could look something like this:
Some other related tips: