Devo salvare un JSon multidimensionale su database mi sono creato il modello, e poi quello che serve per il salvataggio dei dati, funziona se accedo Modello "movements" quindi risco ad accedere al valore di room_number, ma non riesco o meglio non so come fare ad accedere alle proprietà di Customer per salvare i dati name, surname, etc.
Aiuto !!!! per favore. Grazie 1000
Codice per scrivere su DB
var jsonObj = new JavaScriptSerializer().Deserialize<Modello>(result);
foreach (var obj in jsonObj.movements)
{
using (SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["TestJsonString"].ConnectionString))
{
// Open your connection
cn.Open();
//Change the table name here
string sql = "INSERT INTO T_TestJson (sc) VALUES (@sc)";
// Create the Command and Parameter objects.
using (SqlCommand cmd = new SqlCommand(sql, cn))
{
//Loop through the and get of parameter values,
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.Add("@sc", SqlDbType.VarChar).Value = obj.room_number;
cmd.ExecuteNonQuery();
}
}
}
Il mio Modello
//Modello da estrapolare dal JSon
public class Customer
{
[JsonProperty("pms_customer_id")]
public int pms_customer_id { get; set; }
[JsonProperty("checkin_date")]
public string checkin_date { get; set; }
[JsonProperty("checkin_effective")]
public bool checkin_effective { get; set; }
[JsonProperty("checkout_date")]
public string checkout_date { get; set; }
[JsonProperty("checkout_effective")]
public bool checkout_effective { get; set; }
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("surname")]
public string surname { get; set; }
[JsonProperty("sex")]
public string sex { get; set; }
[JsonProperty("fiscalcode")]
public string fiscalcode { get; set; }
[JsonProperty("email")]
public string email { get; set; }
[JsonProperty("phone")]
public string phone { get; set; }
[JsonProperty("address")]
public string address { get; set; }
[JsonProperty("zip")]
public string zip { get; set; }
[JsonProperty("city")]
public string city { get; set; }
[JsonProperty("district")]
public string district { get; set; }
[JsonProperty("state")]
public string state { get; set; }
[JsonProperty("lang_code")]
public string lang_code { get; set; }
[JsonProperty("nationality")]
public string nationality { get; set; }
[JsonProperty("document_number")]
public string document_number { get; set; }
[JsonProperty("document_release_date")]
public string document_release_date { get; set; }
[JsonProperty("document_releaser")]
public string document_releaser { get; set; }
[JsonProperty("birth_date")]
public string birth_date { get; set; }
[JsonProperty("birth_state")]
public string birth_state { get; set; }
[JsonProperty("birth_place")]
public string birth_place { get; set; }
[JsonProperty("birth_district")]
public string birth_district { get; set; }
}
public class Movement
{
[JsonProperty("pms_reservation_id")]
public string pms_reservation_id { get; set; }
[JsonProperty("reservation_date")]
public string reservation_date { get; set; }
[JsonProperty("last_modification_date")]
public string last_modification_date { get; set; }
[JsonProperty("option_date")]
public string option_date { get; set; }
[JsonProperty("reservation_status")]
public string reservation_status { get; set; }
[JsonProperty("room_number")]
public string room_number { get; set; }
[JsonProperty("customers")]
public List<Customer> customers { get; set; }
}
public class Modello
{
[JsonProperty("date")]
public string date { get; set; }
[JsonProperty("movements")]
public List<Movement> movements { get; set; }
}
Modificato da manuelericci il 04 settembre 2017 10.47 -