2016-03-23 14 views
0

:RestSharp ile bir C# Sınıfına dönüşen JSON Nasıl Serileştirilir? Burada bu JSON var

{ 
    "id": "-KDYmr0aI0UmjKRyd465", 
    "name": "No Name", 
    "presentation": { 
     "fields": [ 
     { 
      "defaultValue": "Erste Node", 
      "id": "test", 
      "title": "test", 
      "type": "text" 
     }, 
     { 
      "defaultValue": "Zweite node", 
      "id": "test2", 
      "title": "test2", 
      "type": "text" 
     } 
     ] 
    }, 
    "thumbnail": "/images/defaultimage.png", 
    "updated": "2016-03-23T14:05:32+00:00", 
    "userData": { 
     "test": "Erste Node", 
     "test2": "Zweite node" 
    } 
    } 

userData kullanıcı bazı veriler eklenirse bağlı değişir.

"userData": { 
     "test": "Erste Node", 
     "test2": "Zweite node", 
     "test3": "More testdata" 
    } 

Ayrıca alanları buna göre güncellenmesi olacaktır ama ben bir C# Class o alanları harita nasıl biliyorum: Yani kullanıcı userdata json şu şekilde görünecektir {"test3":"More testdata"} gibi bazı veriler eklendi diyelim. Benim sorum ben bir C# Class olarak bu userData serialize mümkün olacaktır nasıl - ama aynı zamanda buradan RestSharp Client kullanımı ile: hemen hemen benim sınıfları gibi görünecektir nasıl http://restsharp.org/

. Ama

public class Field 
{ 
    public string defaultValue { get; set; } 
    public string id { get; set; } 
    public string title { get; set; } 
    public string type { get; set; } 
} 

public class Presentation 
{ 
    public List<Field> fields { get; set; } 
} 

public class RootObject 
{ 
    public string id { get; set; } 
    public string name { get; set; } 
    public Presentation presentation { get; set; } 
    public string thumbnail { get; set; } 
    public string updated { get; set; } 
    public UserData userData { get; set; } 
} 

cevap

1

Sen seni farkında eminim JSON.NET kullanarak bir C# sınıfına json serialize ... userData haritası nasıl bilmiyorum (ben RestSharp bu kitaplığı kullanan düşünüyorum). geri hizmet kullanım DeserializeObject adresinin json almak ve eşlemek için bir sınıf tanımladığınızda: Bir 'dinamik' türüne benim sınıfını değiştirecek farklı uzunluklarda değiştirebilir userData düğümle başa Şimdi

var json = "{ \"id\": \"-KDYmr0aI0UmjKRyd465\", \"name\": \"No Name\", \"presentation\": { \"fields\": [ { \"defaultValue\": \"Erste Node\", \"id\": \"test\", \"title\": \"test\", \"type\": \"text\" }, { \"defaultValue\": \"Zweite node\", \"id\": \"test2\", \"title\": \"test2\", \"type\": \"text\" } ] }, \"thumbnail\": \"/images/defaultimage.png\", \"updated\": \"2016-03-23T14:05:32+00:00\", \"userData\": { \"test\": \"Erste Node\", \"test2\": \"Zweite node\" } }"; 
var result = JsonConvert.DeserializeObject<RootObject>(json); 

Bu verileri işleyebilir. Yani benim sınıf olacaktı:

public class RootObject 
{ 
    public string id { get; set; } 
    public string name { get; set; } 
    public Presentation presentation { get; set; } 
    public string thumbnail { get; set; } 
    public string updated { get; set; } 
    public dynamic userData { get; set; } 
} 

dinamik userData özelliği yineleme için: buraya

foreach(var data in result.userData) 
{ 
    var name = data.Name; 
    var value = data.Value; 
} 
+0

için Sözlük kullanabilirsiniz düşünüyorum. Bunlarla yineleyebilir miyim veya bunları benim durumumda nasıl kullanırım? – Kazoooka

+0

Bazı örnek kodla güncellendi. –

2

biri hızlı uç deyişle, kullanıcı verileri için size Sözlük kullanmalıdır. Bu dinamik olarak birçok userData eklemek için esneklik sağlayacaktır (anahtar değer çiftleri). Önemli olan bu, JSON'a normal bir sınıf gibi serileştirilecek.

1

Sana userData özelliği Böyle dinamik sınıflar hiç kullanmadıysanız

public class Field 
{ 
    public string defaultValue { get; set; } 
    public string id { get; set; } 
    public string title { get; set; } 
    public string type { get; set; } 
} 

public class Presentation 
{ 
    public List<Field> fields { get; set; } 
} 

public class RootObject 
{ 
    public string id { get; set; } 
    public string name { get; set; } 
    public Presentation presentation { get; set; } 
    public string thumbnail { get; set; } 
    public string updated { get; set; } 
    public Dictionary<string, string> userData { get; set; } 
}