如何使用c#和Json.net框架获取Json上的字段和it值?

如何使用c#和Json.net框架获取Json上的字段和it值?,c#,json,xamarin,C#,Json,Xamarin,我有以下代码: 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((JObje

我有以下代码:

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]);

}
我需要一种方法来添加数据库中每个对象中的每个字段。 例如,我需要说like var wineId=(JObject)items[i].wineId;
我需要得到我想要的字段的值。。。我该怎么做呢?

就我个人而言,我发现为数据创建对象要比使用
JObject
/
JArray
对象容易得多。因此,例如,您的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; }
}

对于protip,您可以使用生成模型。

您可以通过索引或键引用数组中的每个元素

var val = items[0].Value;
// or
var val = items["IdUva"].Value;
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);
}
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 */
}
var val = items[0].Value;
// or
var val = items["IdUva"].Value;