Json Net层次结构类

时间:2014-03-05 07:39:43

标签: json vb.net json.net json-deserialization

我在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
    }
}

我如何得到工人的长度(总数)并用它循环,这样我就能得到每个工人的记录?因为工人可以继续加起来。

提前致谢。

2 个答案:

答案 0 :(得分:0)

当你试图得到工人的总数并且你说工人可以不断加起来时,我会建议你重组你的JSON(如果你有修改的范围),这样工人就可以像每次需要新工作人员添加到JSON时,array而不是像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);
}

答案 1 :(得分:0)

如果您的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);
}