23 Kasım 2016 Çarşamba

NewTonsoft JToken Sınıfı

Giriş
Şu satırı dahil ederiz.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Bu sınıf json ve Linq'i beraber kullanabilmemizi sağlar

FirstOrDefault metodu
Şöyle yaparız.
JToken token = ...;

// retrieve the champion desired object using the Linq FirstOrDefault method. 
// This method will return the first object that matches the given query,
// or return null if it does not find a match.
JToken champion = token.FirstOrDefault(c=> (int)c["id"] == 36);

if (champion != null)
{
  // retrieve the stats object
  JToken stats = champion["stats"];

  // read the totalSessionsWon field from the object.
  int totalSessionsWon = (int) stats["totalSessionsWon"];
}
operator [] metodu
Şöyle yaparız.
var json = @"
    {
        ""someObj"": 5
    }
";
var result = JsonConvert.DeserializeObject<JToken>(json);
var t = result["someObj"]; //contains 5

Hiç yorum yok:

Yorum Gönder