2014-01-30 21 views
1

我有這樣的代碼:如何使用c#和json.net框架獲取JSON上的字段和值?

var json = Newtonsoft.Json.Linq.JObject.Parse("{items:"+response.Content+"}"); 

Console.WriteLine(json.Count); 

var items = (JArray)json["items"]; 

for (int i = 0; i < items.Count; i++) { 

Console.WriteLine("ae"); 

Console.WriteLine((JObject)items[i]); 

} 

這裏是response.Content我有:

[{"IdUva":36,"Uva":"TESTES","IdPais":249,"Pais":"Australia","IdProduto":5114,"Descricao":"ABEL PINCHARD COTES DU RHONE","Estoque":-467700.801,"idVinhoDetalhes":84,"Produtor":"TEST","Regiao":"TESTE","Tipo":"BRANCO","Safra":"2011","Teor":99.00,"FichaTecnica":"FHGFGHFFJHFJFJHGFJFJHF","Servico":"FRANGO","Mapa":"Koala.jpg","Foto":"Chrysanthemum.jpg","Preco":1.00,"CodigoPais":36,"Bandeira":"Australia.png"}] 

我需要一種方法來添加喜歡每一個我在每一個對象在數據庫中的字段。 例如我需要說像var wineId =(JObject)items [i] .WineID; 我需要獲得我想要的字段的值...我該怎麼做?

回答

1

就我個人而言,我發現爲數據創建對象要比使用對象更容易創建對象。所以,例如你的JSON將轉化中是這樣的:

public class Item 
{ 
    public int IdUva { get; set; } 
    public string Uva { get; set; } 
    public int IdPais { get; set; } 
    public string Pais { get; set; } 
    public int IdProduto { get; set; } 
    public string Descricao { get; set; } 
    public double Estoque { get; set; } 
    public int idVinhoDetalhes { get; set; } 
    public string Produtor { get; set; } 
    public string Regiao { get; set; } 
    public string Tipo { get; set; } 
    public string Safra { get; set; } 
    public double Teor { get; set; } 
    public string FichaTecnica { get; set; } 
    public string Servico { get; set; } 
    public string Mapa { get; set; } 
    public string Foto { get; set; } 
    public double Preco { get; set; } 
    public int CodigoPais { get; set; } 
    public string Bandeira { get; set; } 
} 

public class RootObject 
{ 
    public List<Item> items { get; set; } 
} 

然後我們就可以反序列化:

RootObject json = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(
    "{\"items\":" + response.Content + "}" 
); 

// iterate over the items 
foreach (Item item in json.items) 
{ 
    // do what you want with them 
    Console.WriteLine(item.Pais); 
} 

當然,如果你已經有一個「項目」對象(從數據庫)你可以使用它,讓事情變得非常簡單。如果JSOn中的屬性與您的模型屬性不完全一致,則可以使用JsonProperty屬性對它們進行修飾。例如,我可以有一個英文模式:

public class Item 
{ 
    [JsonProperty("IdUva")] 
    public int GrapeID { get; set; } 

    [JsonProperty("Uva")] 
    public string Grape { get; set; } 

    [JsonProperty("IdPais")] 
    public int ParentID { get; set; } 

    [JsonProperty("Pais")] 
    public string Parent { get; set; } 

    [JsonProperty("IdProduto")] 
    public int ProductID { get; set; } 

    [JsonProperty("Descricao")] 
    public string Description { get; set; } 

    [JsonProperty("Estoque")] 
    public double Stock { get; set; } 

    [JsonProperty("idVinhoDetalhes")] 
    public int WineDetailID { get; set; } 

    [JsonProperty("Produtor")] 
    public string Producer { get; set; } 

    [JsonProperty("Regiao")] 
    public string Region { get; set; } 

    [JsonProperty("Tipo")] 
    public string Type { get; set; } 

    [JsonProperty("Safra")] 
    public string Harvest { get; set; } 

    /* etc */ 
} 

而對於一個普羅蒂普,您可以使用json2csharp生成您的模型。

1

您可以通過其索引或其主要

var val = items[0].Value; 
// or 
var val = items["IdUva"].Value; 
請參閱您的陣列中的每個元素
相關問題