Rutuparna Panda
Rutuparna Panda

Reputation: 13

How to parse JSON object using C#

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!

Upvotes: 0

Views: 227

Answers (2)

Rutuparna Panda
Rutuparna Panda

Reputation: 13

Changes made in model class:

public class dlregistrationdata
{
    public string cph_id { get; set; }
    public string phone_no { get; set; }
}

public class RequestRegistrationAPI { 
    public string user_name { get; set; }
    public string user_password { get; set; }
    public string user_phone_number { get; set; }
    public string name { get; set; }
    public int status { get; set; }
    public string[] cph_id { get; set; }
}

Changes made in code:

  using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
  {
      List<string> cphid = new List<string>();
      string phone_no = "";
      foreach (dlregistrationdata data in obj)
      {
          cphid.Add(data.cph_id);
          phone_no = data.phone_no;
      }

     RequestRegistrationAPI request = new RequestRegistrationAPI();
     request.user_name = email;                                
     request.user_password = password;
     request.user_phone_number = phone_no;
     request.name = name;
     request.cph_id = cphid.ToArray();
     request.status = 1;
                            
     streamWriter.Write(JsonConvert.SerializeObject(request));
 }

This works perfectly for me.

Upvotes: 1

persian-theme
persian-theme

Reputation: 6638

change classes to :

public class dlregistrationdata
{
    public string cph_id { get; set; }
    public string phone_no { get; set; }
} 

now change code to :

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

Upvotes: 0

Related Questions