3

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

9
  • A little more info is needed to help with this... whats the command you use to deserialize? Can you post a snippet of json Commented Jul 17, 2020 at 2:07
  • 1
    the issue is not in the model code, it is in how you're calling Json.Net; can you please include that code? Commented Jul 17, 2020 at 2:08
  • can you confirm if this is the json you are using? If so, you need to remove the extra curly braces from start and end Commented Jul 17, 2020 at 2:13
  • Where are you getting the json from... what's the method u use Commented Jul 17, 2020 at 2:27
  • This is not valid JSON, please verify that you have indeed posted the right input in your question, and if you have, there's your problem. Commented Jul 17, 2020 at 8:19

2 Answers 2

4

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Please how would I be able to remove the extra curly braces from the JSON? It comes from an API response
When I tried to clean up the json,I got this response "Newtonsoft.Json.Linq.JObject' does not contain a definition for 'Trim'"
So, as it stands now, I have not found a way to remove those extra enclosing curly braces.
Convert the JSON to a string and remove them yourself...
1

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);
}

Result: enter image description here

1 Comment

Thank you very much. This is elaborate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.