如何迭代包含两个 class 列表的 class? - RestSharp
How to iterate over a class that contains list of two classes? - RestSharp
这是我的数据:
{
"-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"
}
}
}
这是我的交易 class :
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;
}*/
}
此代码为我提供了作为唯一值的键和作为交易的值。
如果我尝试遍历事务 class,我收到一条错误消息,指出必须实现 IEnumerable。我如何遍历事务 class 并获取值。
谢谢!
这不是 两个 类 的列表。您得到的是 Transactions
类型的 Object
(Value),它包含两个属性:
item.Value.bizcardData
item.Value.transData
如果您不关心键,您可以迭代字典的值(事务)。
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;
}
这是我的数据:
{
"-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"
}
}
}
这是我的交易 class :
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;
}*/
}
此代码为我提供了作为唯一值的键和作为交易的值。
如果我尝试遍历事务 class,我收到一条错误消息,指出必须实现 IEnumerable。我如何遍历事务 class 并获取值。
谢谢!
这不是 两个 类 的列表。您得到的是 Transactions
类型的 Object
(Value),它包含两个属性:
item.Value.bizcardData
item.Value.transData
如果您不关心键,您可以迭代字典的值(事务)。
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;
}