【问题标题】:How to get the fields and it values on Json with c# and json.net framework?如何使用 c# 和 json.net 框架获取 Json 上的字段及其值?
【发布时间】:2014-01-30 19:56:51
【问题描述】:

我有这个代码:

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

}

这是我的响应。内容:

[{"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; 我需要获取我想要的字段的值...我该怎么做?

【问题讨论】:

    标签: c# json xamarin


    【解决方案1】:

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

    var val = items[0].Value;
    // or
    var val = items["IdUva"].Value;
    

    【讨论】:

      【解决方案2】:

      就我个人而言,我发现为数据创建对象比使用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; }
      }
      

      然后我们可以反序列化它:

      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 生成模型。

      【讨论】:

        猜你喜欢
        • 2022-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-11
        • 2015-11-21
        • 1970-01-01
        相关资源
        最近更新 更多