C# JSON Veri Dönüştürme

  • 764
  • (2)
  • (5)
  • 20 Nis 2018

JSON verileri Web API özelliği içeren projelerde oldukça kullanışlıdır. Web tarafında metotlarla belirlediğiniz işlemleri masaüstü, mobil veya başka bir web arayüzünde kullanarak veritabanınıza doğrudan erişimi kesebilirsiniz.

Aşağıdaki gibi bir JSON veri modeli C# üzerinde sınıf modelleri ile oluşturulabilir.


{
    "id": 104,
    "appName": "Serbest Muhasebe",
    "appVersion": "v2.0.1",
    "assembly": "SerbestMuhasebe.exe",
    "publisher": "RedWall Yazılım",
    "releaseDate": "2015-07-18T12:22:18.000Z"
}

Tools menüsünden NuGet Package Manager içerisinde Manage NuGet Packages for Solution ile girdiğiniz arayüzden Newtonsoft.Json kütüphanesini yüklemeniz gerekiyor.


public class AppModel
{
    [JsonProperty("id")]
    public int ID { get; set; }
    [JsonProperty("appName")]
    public string ApplicationName { get; set; }
    [JsonProperty("appVersion")]
    public string ApplicationVersion { get; set; }
    [JsonProperty("assembly")]
    public string Assembly { get; set; }
    [JsonProperty("publisher")]
    public string Publisher { get; set; }
    [JsonProperty("releaseDate")]
    public DateTime ReleaseDate { get; set; }
}

Web API üzerinde veri modelini okumak için aşağıdaki gibi bir web metot kullanılacaktır. Bu tarz web metotları test etmek için Postman programını kullanabilirsiniz.


[HttpGet]
[Route("api/application/get/{id}")]
public async Task<IHttpActionResult> Get(int id)
{
    if (id < 1)
        return Conflict();
    // ...
    AppModel db_application = await DB.GetApplication(id);
    return Json(db_application);
}

Veri modelini Json halinde Web API üzerine göndermek istediğinizde ise HttpPost türü bir web metot kullanmalısınız.


[HttpPost]
[Route("api/application/post")]
public async Task<IHttpActionResult> Post([FromBody] AppModel application)
{
    if (application == null)
        return Conflict();
    else if (string.IsNullOrEmpty(application.ApplicationName))
        return Content(HttpStatusCode.Conflict, "Uygulama için isim bilgisi girilmedi");
    // ...
    AppModel db_application = await DB.GetApplication(id);
    return Ok();
}

Alınan veya gönderilen veri modelini dönüştürmek için yine Newtonsoft.Json kütüphanesi kullanılacaktır. Örneğin Web API ile http://localhost:xxx/api/application/get/54 linkinden bir veri çektiniz bunu Json' dan veri modeline dönüştürmek için aşağıdaki kodu uygulayabilirsiniz.


//WebAPI isminde HttpClient ile veri okuyan ve gönderen static bir metot olsun
string getJson = WebAPI.GetApplicationJson("http://localhost:xxx/api/application/get/54");
AppModel application = Newtonsoft.Json.JsonConvert.DeserializeObject<AppModel>(getJson);

string postJson = Newtonsoft.Json.JsonConvert.SerializeObject(application);
WebAPI.PostApplicationJson(postJson);

İlişkili İçerikler

Web sayfanız üzerinde XMLHttpRequest nesnesi ile veri gönderip alabilmeniz mümkündür. JQuery kütüphanesi ve ajax nesnesi kullanmadan veri alışverişi yapabilirsiniz.

Paylaşın
Etiket Bulutu