Reputation: 1970
I have the following JSON object and I have been trying to convert it to a datatype, Say MessageData in C# but I keep getting the error that says
The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject(string)' has some invalid arguments.
The JSON is as follows:
{
{
"SMSMessageData": {
"Message": "Sent to 1/1 Total Cost: NGN 4.4000",
"Recipients": [
{
"statusCode": 102,
"number": "+2348033776502",
"cost": "NGN 4.4000",
"status": "Success",
"messageId": "ATXid_b96d58d359d2e12ad0b9696cee7630ce"
}
]
}
}
}
MessageResponse is as follows below
public class MessageResponse
{
public List<SMSMessageData> Data { get; set; }
}
public class SMSMessageData
{
public string Message { get; set; }
public List<SMSRecipient> Recipients { get; set; }
}
public class SMSRecipient
{
public int StatusCode { get; set; }
public string PhoneNumber { get; set; }
public string Cost { get; set; }
public string StatusMessage { get; set; }
public string MessageId { get; set; }
}
This is how I am trying to deserialize it
var response= JsonConvert.DeserializeObject<MessageResponse>(json)
From the comments, it seems the problem is from the extra enclosing curly braces which I have not found a way to remove so far. I am not able to do json.ToString() and without converting it to string, I am not able to apply Trim or any other String methods to remove the extra enclosing curly braces.
Please I need assistance on how to successfully convert this to the MessageResponse data.
I am running it on ASP.Net Core 3.1
Thank you
Upvotes: 3
Views: 12306
Reputation: 36645
Firstly,your model is not correct,it should be:
public class MessageResponse
{
public SMSMessageData SMSMessageData { get; set; } //change this...
}
public class SMSMessageData
{
public string Message { get; set; }
public List<SMSRecipient> Recipients { get; set; }
}
public class SMSRecipient
{
public int StatusCode { get; set; }
public string PhoneNumber { get; set; }
public string Cost { get; set; }
public string StatusMessage { get; set; }
public string MessageId { get; set; }
}
For easy testing,the api I designed like below:
[Route("api/[controller]")]
public class ValuesController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
var data = System.IO.File.ReadAllText("test.json");//in test.json is your json string
return Ok(data);
}
}
Call the api:
[HttpGet]
public async Task Get()
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("https://localhost:44331/api/values");
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
json = json.Trim().TrimStart('{').TrimEnd('}');
var data = JsonConvert.DeserializeObject<MessageResponse>(json);
}
Upvotes: 1
Reputation: 65554
According to https://json2csharp.com/ your JSON is malformatted, I can see you need to remove the extra 2 surrounding curly brackets.
If the JSON comes from an API response you could trim the brackets with a string operation before deserializing, eg
var myJsonResponse = json.ToString().Trim().TrimStart('{').TrimEnd('}');
This is the correct JSON:
{
"SMSMessageData": {
"Message": "Sent to 1/1 Total Cost: NGN 4.4000",
"Recipients": [
{
"statusCode": 102,
"number": "+2348033776502",
"cost": "NGN 4.4000",
"status": "Success",
"messageId": "ATXid_b96d58d359d2e12ad0b9696cee7630ce"
}
]
}
}
The Object Model to represent the JSON:
public class Recipient {
public int statusCode { get; set; }
public string number { get; set; }
public string cost { get; set; }
public string status { get; set; }
public string messageId { get; set; }
}
public class SMSMessageData {
public string Message { get; set; }
public List<Recipient> Recipients { get; set; }
}
public class Root {
public SMSMessageData SMSMessageData { get; set; }
}
Then calling it:
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse)
Upvotes: 4