2013-02-18 30 views
6

tôi đang làm việc với chuỗi JSON sauLàm thế nào để deserialize chuỗi json phản đối danh sách trong C# dot

{ 
"transactions": 
[ 
    { 
    "paymentcharge":"0.0", 
    "amount":352, 
    "id":13418, 
    "shippingcharge":35, 
    "shippingtype":2, 
    "status":2, 
    "paymenttype":1, 
    "date":"2012-10-06 16:15:28.0" 
    }, 
    { 
    "paymentcharge":"0.0", 
    "amount":42455, 
    "id":16305, 
    "shippingcharge":0, 
    "shippingtype":2, 
    "status":2, 
    "paymenttype":2, 
    "date":"2012-11-30 09:29:29.0" 
    }, 
    { 
    "paymentcharge":"1.0", 
    "amount":42456, 
    "id":16305, 
    "shippingcharge":0, 
    "shippingtype":2, 
    "status":2, 
    "paymenttype":2, 
    "date":"2012-11-30 09:29:29.0" 
    } 
], 
"count":3 
} 

Tôi có một cấu trúc lớp như sau để phân tích và cảm nhận dữ liệu json

class clsSalesTran 
{ 
    public double paymentcharge { get; set; } 
    public double amount { get; set; } 
    public long id { get; set; } 
    public int shippingcharge { get; set; } 
    public int shippingtype { get; set; } 
    public int status { get; set; } 
    public int paymenttype { get; set; } 
    public DateTime date { get; set; } 
} 

Tôi có thể deserialize chuỗi JSON ở trên vào Danh sách như thế nào?

Tôi đang sử dụng Newtonsoft.Json để deserialize.

Trả lời

11

đầu tiên tạo ra một lớp:

public class SalesTransactions 
{ 
    public List<clsSalesTran> transactions {get;set;} 
    public int count{get;set;} 
} 

Sau đó sử dụng,

JsonConvert.DeserializeObject<SalesTransactions>(inputString) 
+0

cách trang trí một thành viên Lớp để khớp với đối tượng JSON? Đối với Ví dụ: Một đối tượng JSON chứa {1.024.720: "somePic.jpg"} Tôi có một lớp Lớp nào ảnh { public string HdImage; } –

0
class WeapsCollection 
{ 
    public Dictionary<string, WeaponDetails> Weapons { get; set; } 

} 

class WeaponList 
{ 
    public WeaponDetails AEK { get; set; } 
    public WeaponDetails XM8 { get; set; } 
} 

class WeaponDetails 
{ 
    public string Name { get; set; } 
    public int Kills { get; set; } 
    public int Shots_Fired { get; set; } 
    public int Shots_Hit { get; set; } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     string json = @" 
     { 
      'weapons': 

        { 
         'aek': 
          { 
           'name':'AEK-971 Vintovka', 
           'kills':47, 
           'shots_fired':5406, 
           'shots_hit':858 
          }, 
         'xm8': 
          { 
           'name':'XM8 Prototype', 
           'kills':133, 
           'shots_fired':10170, 
           'shots_hit':1790 
          }, 
        } 

     }"; 

     WeapsCollection weps = JsonConvert.DeserializeObject<WeapsCollection>(json); 
     Console.WriteLine(weps.Weapons.First().Value.Shots_Fired);    

     Console.ReadLine(); 

    } 
} 

Trả lời trở lại trong trường hợp bất kỳ vấn đề.

4

Tạo một lớp như sau
Bằng cách tạo danh sách các lớp 'clsSalesTran' và một biến cho 'Đếm'

Lưu ý: JsonProperty là bắt buộc từ Json Chuỗi bạn

public class SalesTransactions 
{ 
    [JsonProperty("transactions")] 
    public List<clsSalesTran> transactions {get;set;} 
    public int count{get;set;} 
} 

Sau đó, bạn có thể sử dụng lớp này như dưới đây để deserialize

SalesTransactions st = JsonConvert.DeserializeObject<SalesTransactions>(inputString) 

Sử dụng t đối tượng Deserialized của anh ấy như bên dưới

double paymentcharge = st.transactions[0].paymentcharge; 
Các vấn đề liên quan