我在 VB.Net 上使用Newtonsoft.Json,并且在json反序列化方面遇到了问题。
在这方面完全是一个菜鸟。一个简单的类层次结构,我可以做到...但是像下面这样更复杂的人,完全是个白痴。
有人可以帮助我从这个 JSON 构建序列化类层次结构吗?
{
"user": {
"user_id": 123456,
"total": 100,
"rewards": 0
},
"workers": {
"1": {
"worker_name": "rainfall_home",
"rate": 0,
"shares": 0
},
"2": {
"worker_name": "rainfall_office",
"rate": 7358.71,
"shares": 96564
},
"3": {
"worker_name": "rainfall_rig",
"rate": 0,
"shares": 77208
},
"4": {
"worker_name": "rainfall_s11",
"rate": 178365.22,
"shares": 3760356
},
"5": {
"worker_name": "rainfall_s12",
"rate": 196537.7,
"shares": 4152710
}
},
"total": {
"total_user": 5,
"record_date": 2
}
}
我如何获取工人长度(总)并与之循环,以便我可以获取每个工人的记录?因为工人可以不断加起来。
提前谢谢。
当您试图获取工作线程总数并且您说工作线程可以继续累积时,那么我建议您重组您的 JSON(如果您有修改的范围),以便可以将工作线程作为array
进行管理,而不是每次需要新工作线程添加到 JSON 时都像object
一样添加它。请参阅下面建议的 JSON:
{
"user": {
"user_id": 123456,
"total": 100,
"rewards": 0
},
"workers": [
{
"worker_name": "rainfall_home",
"rate": 0,
"shares": 0
},
{
"worker_name": "rainfall_office",
"rate": 7358.71,
"shares": 96564
},
{
"worker_name": "rainfall_rig",
"rate": 0,
"shares": 77208
},
{
"worker_name": "rainfall_s11",
"rate": 178365.22,
"shares": 3760356
},
{
"worker_name": "rainfall_s12",
"rate": 196537.7,
"shares": 4152710
}
],
"total": {
"total_user": 5,
"record_date": 2
}
}
现在,您的新分类如下:
public class RootObject
{
public User user { get; set; }
public List<Worker> workers { get; set; }
public Total total { get; set; }
}
public class User
{
public int user_id { get; set; }
public int total { get; set; }
public int rewards { get; set; }
}
public class Worker
{
public string worker_name { get; set; }
public double rate { get; set; }
public int shares { get; set; }
}
public class Total
{
public int total_user { get; set; }
public int record_date { get; set; }
}
现在,您可以轻松地遍历每个工人以获取详细信息,如下所示:
string json = "YOUR_JSON_STRING";
RootObject rootObject = JsonConvert.DeserializeObject<RootObject> ( json );
Console.WriteLine("Total no. of workers: {0}", rootObject.workers.Count);
foreach ( Worker worker in rootObject.workers )
{
Console.WriteLine("Name: {0}, Rate: {1}, Shares: {2}", worker.worker_name, worker.rate.ToString("F2", CultureInfo.InvariantCulture), worker.shares);
}
如果您的 JSON 数据来自外部 API,则可以按如下方式对其进行分类:
public class RootObject
{
public User user { get; set; }
public Dictionary<string, Worker> workers { get; set; }
public Total total { get; set; }
}
public class User
{
public int user_id { get; set; }
public int total { get; set; }
public int rewards { get; set; }
}
public class Worker
{
public string worker_name { get; set; }
public double rate { get; set; }
public int shares { get; set; }
}
public class Total
{
public int total_user { get; set; }
public int record_date { get; set; }
}
现在,您可以轻松地遍历每个工人以获取详细信息,如下所示:
string json = "YOUR_JSON_STRING";
RootObject rootObject = JsonConvert.DeserializeObject<RootObject> ( json );
Console.WriteLine("Total no. of workers: {0}", rootObject.workers.Count);
foreach ( KeyValuePair<string, Worker> worker in rootObject.workers )
{
Console.WriteLine("Name: {0}, Rate: {1}, Shares: {2}", worker.Value.worker_name, worker.Value.rate.ToString("F2", CultureInfo.InvariantCulture), worker.Value.shares);
}