I want to post data to the database where I am fetching the data in a JSON format.
Here is the following JSON string:
"[{"cph_id":"67/123/7894","phone_no":"0000623019"},
{"cph_id":"69/213/1234","phone_no":"0000623019"}]"
I have also created the following classes:
public class RootObject
{
public List<dlregistrationdata> data { get; set; }
}
public class dlregistrationdata
{
public string cph_id[] { get; set; }
public string phone_no[] { get; set; }
}
I try to deserialize using the following command:
try
{
var obj = JsonConvert.DeserializeObject<List<dlregistrationdata>>(result);
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://172.61.25.30/CPRestApi/api/user/register");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
if (email != null)
{
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
dynamic arr = new JObject();
for (int i = 0; i < obj.Count; i++)
{
arr.cph_id = obj[i].cph_id;
arr.user_name = email;
arr.user_phone_number = obj[i].phone_no;
arr.user_password = password;
arr.status = 1;
arr.name = name;
}
streamWriter.Write(arr);
}
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
if (httpResponse.StatusCode == System.Net.HttpStatusCode.Created)
{
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result1 = streamReader.ReadToEnd();
}
return RedirectToLocal("Index");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
ViewBag.ErrorMessage = "User Already Registered";
ModelState.AddModelError("", "User Already Registered");
return View(model);
}
But I am getting the error: "converting value "67/123/7894" to type 'System.String[]'. Path '[0].cph_id', line 1, position 24"
Any help will be highly appreciated.
Thank You!
JsonPropertyattributes to specify the names in the Json rather than violating c# naming conventions to match your c# to the json