I need help deserializing a response. (I'm making a call to Data247 on a phone number, and the api returns the mobile phone carrier). The api call is working and I'm getting back a valid response that looks like this:
{
"response": {
"status": "OK",
"results": [
{
"phone": "11234567890",
"wless": "y",
"carrier_name": "Blue Licenses Holding, LLC",
"carrier_id": 6,
"sms_address": "1234567890@txt.att.net",
"mms_address": "1234567890@mms.att.net"
}
]
}
}
This is in a C#.NET winforms app. To test this, you'd have to have an access key from Data247. Everything is working through the api call. My problem is with this line:
Data247 data247 = JsonConvert.DeserializeObject<Data247>(response.Content);
...and so I don't think my class is structured correctly. ...or maybe I'm not deserializing correctly. My code looks like this:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using RestSharp;
using Newtonsoft.Json;
namespace myTest
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnVerify_Click(object sender, EventArgs e)
{
try
{
string request = txtEndpoint.Text;
request += "?key=" + txtAccessKey.Text;
request += "&api=CU";
request += "&phone=" + txtPhone.Text;
var client = new RestClient(request);
var response = client.Execute(new RestRequest());
Data247 data247 = JsonConvert.DeserializeObject<Data247>(response.Content);
MessageBox.Show(response.Content);
Debug.Print(response.Content);
}
catch (Exception ex)
{
Debug.Print(ex.ToString());
}
}
public class Data247
{
Response response { get; set; }
public class Response
{
public string status { get; set; }
Results results { get; set; }
public class Results
{
List<Phone> phones = new List<Phone>();
public Phone phone { get; set; }
public class Phone
{
public string phone { get; set; }
public string wless { get; set; }
public string carrier_name { get; set; }
public string carrier_id { get; set; }
public string sms_address { get; set; }
public string mms_address { get; set; }
}
}
}
}
}
}