2014-01-30 23 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; 
请参阅您的阵列中的每个元素
相关问题