2013-02-18 13 views
6

Sto lavorando con la seguente stringa JSONcome deserializzare stringa JSON di opporsi lista in 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 
} 

Ho una struttura di classe come segue per l'analisi e sentendo i dati 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; } 
} 

Come posso deserializzare la stringa JSON sopra nell'elenco?

Sto usando Newtonsoft.Json per deserializzare.

risposta

11

prima creare un'altra classe:

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

Quindi utilizzare,

JsonConvert.DeserializeObject<SalesTransactions>(inputString) 
+0

come decorare un membro di Classe per abbinare l'oggetto JSON? Per Es: Un oggetto JSON contiene {1.024.720: "somePic.jpg"} Ho una classe Classe pubblica Immagine {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(); 

    } 
} 

risposta indietro in caso di eventuali problemi.

4

Creare una classe, come di seguito
Creando l'elenco dei class 'clsSalesTran' e una variabile per 'conte'

Nota: JsonProperty è obbligatoria dal JSON String

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

Poi è possibile utilizzare questa classe come sotto per deserializzare

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

Utilizzare t Oggetto deserializzato come sotto

double paymentcharge = st.transactions[0].paymentcharge; 
Problemi correlati