如何迭代一个类,包含两个类的列表?——RestSharp

本文关键字:RestSharp 包含两 列表 一个 何迭代 迭代 | 更新日期: 2023-09-27 18:10:38

这是我的数据:

{
  "-JxsJFiGBqQz1KQmmR0i" : {
    "bizcardData" : {
      "company" : "Tesla",
      "designation" : "Developer",
      "email" : "phani@tesla.com",
      "name" : "Phani",
      "phone" : "5135921241"
    },
    "transData" : {
      "date" : "15-08-29",
      "location" : "39.1395996,-84.5295417",
      "tag" : "sender",
      "time" : "03:17:00"
    }
  },
  "-JxsJKnJIVTFQWE1aSOr" : {
    "bizcardData" : {
      "company" : "Spotify",
      "designation" : "Designer",
      "email" : "komarapa@spotify.com",
      "name" : "Phani Komaravolu",
      "phone" : "5135921241"
    },
    "transData" : {
      "date" : "15-08-29",
      "location" : "39.1395996,-84.5295417",
      "tag" : "sender",
      "time" : "03:17:21"
    }
  }
}

这是我的事务类:

public class Transactions
    {
            public BizcardData bizcardData { get; set; }
            public TransData transData { get; set; }
    }
public class BizcardData
{
    public string company { get; set; }
    public string designation { get; set; }
    public string email { get; set; }
    public string name { get; set; }
    public string phone { get; set; }
}
public class TransData
{
    public string date { get; set; }
    public string location { get; set; }
    public string tag { get; set; }
    public string time { get; set; }
}

我使用RestSharp:

反序列化
var resultList = SimpleJson.DeserializeObject<Dictionary<string, Transactions>>(content);
            Console.WriteLine ("Deserialized resultList"+resultList);
            foreach(var item in resultList)
            {
                var key = item.Key;
                var value = item.Value;
            /*  foreach(Transactions go in item.Value)
                {
                    var bizcardData = go.bizcardData;
                    var transData = go.transData;
                }*/
            }

此代码将Key作为唯一值,并将值作为Transaction。

如果我尝试迭代事务类,我得到一个错误说,必须实现IEnumerable。我如何在事务类上迭代并获得值。

谢谢!

如何迭代一个类,包含两个类的列表?——RestSharp

如果你不关心键,你可以只迭代字典的值(事务)。

var resultList = SimpleJson.DeserializeObject<Dictionary<string, Transactions>>(content);
Console.WriteLine ("Deserialized resultList"+resultList);
foreach(var transaction in resultList.Values)
{
     var bizcardData = transaction.bizcardData;
     var transData = transaction.transData;
}

如果你真的关心键,那就去掉第二个循环。

var resultList = SimpleJson.DeserializeObject<Dictionary<string, Transactions>>(content);
Console.WriteLine ("Deserialized resultList"+resultList);
foreach(var pair in resultList)
{
     var key = pair.Key;
     var transaction = pair.Value;
     var bizcardData = transaction.bizcardData;
     var transData = transaction.transData;
}

它不是两个类的列表。你得到的是Transactions类型的Object (),它包含两个属性:

item.Value.bizcardData
item.Value.transData