将JSON数据从Web映射到.NET类,其中数据因每个API请求而异

时间:2015-01-20 11:08:26

标签: c# json class

我正在使用http请求从asp.net网站拨打api。 api以JSON格式返回数据。

  

请求是这样的:   GET /统计/ {粒度} / {CUSTOMER_NUMBER} / {timestamp_start} / {timestamp_end}。

问题是返回的数据取决于您在请求中输入的时间戳。

这是一个示例:

{
    "success": {
        "1310860635": {
            "1396310400": {
                "iPad": 2317357,
                "Other": 564
            },
            "1396915200": {
                "iPad": 2538835,
                "Other": 372
            },
            "1396656000": {
                "iPad": 2349576,
                "Other": 1136
            },
            "1398729600": {
                "iPad": 1648157,
                "Other": 163,
                "HTC Streaming P": 32
            },
            "1397606400": {
                "iPad": 2018706,
                "Other": 788
            },
            "1396396800": {
                "iPad": 2477197,
                "Other": 608,
                "Spider": 2
            },
            "1397692800": {
                "iPad": 2144772,
                "Other": 576
            },
            "1398816000": {
                "iPad": 2117556,
                "Other": 1838,
                "HTC Streaming P": 14
            },
            "1398124800": {
                "iPad": 2662858,
                "Other": 306,
                "Spider": 3
            },
            "1398038400": {
                "iPad": 2658527,
                "Other": 565,
                "HTC Streaming P": 98,
                "Spider": 1
            },
            "1397260800": {
                "iPad": 1696601,
                "Other": 218
            },
            "1396483200": {
                "iPad": 2431192,
                "Other": 204
            },
            "1398297600": {
                "iPad": 2186146,
                "Other": 567
            },
            "1397001600": {
                "iPad": 330815,
                "Other": 32
            },
            "1398211200": {
                "iPad": 2457731,
                "Other": 381
            },
            "1397347200": {
                "iPad": 2037233,
                "Other": 175
            },
            "1397779200": {
                "iPad": 2438668,
                "Other": 445,
                "HTC Streaming P": 40
            },
            "1396569600": {
                "iPad": 517843,
                "Other": 52,
                "Spider": 1
            },
            "1397433600": {
                "iPad": 1517589,
                "Other": 161
            },
            "1398902400": {
                "iPad": 2059013,
                "Other": 1878
            },
            "1397174400": {
                "iPad": 338428,
                "Other": 57
            },
            "1397520000": {
                "iPad": 2024273,
                "Other": 214
            },
            "1397088000": {
                "iPad": 275725,
                "Other": 21
            },
            "1398384000": {
                "iPad": 2511796,
                "Other": 586
            },
            "1397865600": {
                "iPad": 2585367,
                "Other": 613
            },
            "1398470400": {
                "iPad": 2558398,
                "Other": 327
            },
            "1398556800": {
                "iPad": 1447445,
                "Other": 97
            },
            "1398643200": {
                "iPad": 1475406,
                "Other": 161
            },
            "1396742400": {
                "iPad": 2838708,
                "Other": 484
            },
            "1396828800": {
                "iPad": 2502484,
                "Other": 513
            },
            "1397952000": {
                "iPad": 2724871,
                "Other": 371
            }
        }
    }
}

有没有办法将这个json数据映射到C#类,它不应该依赖于每个请求变化的时间戳?

2 个答案:

答案 0 :(得分:1)

//只是一个快速的片段

        //Assuming you have this class for your destinationn object
        public class iPadClass
        {
            public string ipad;
            public string other;
        }

        List<iPadClass> ipadList = new List<iPadClass>();

        //since your source is not a typed class
        IEnumerable dataList = getDataFromWebAPI() 

        foreach (var row in dataList)
        {
                ipadList.Add(new iPadClass() { ipad = row[0], other = row[1]});

               //or whichever works for you

                ipadList.Add(new iPadClass() { ipad = row.items[0], other = row.items[1]});
            }
        }

实验还将dataList用作List而不是IEnumerable

如果您希望自己的来源也是类型类,请参阅此处有关how to convert a JSON structure to another and add a extra field in to it

的答案

答案 1 :(得分:1)

这样做。

public class Products
{
    public string ProductName;
    public int Units;
}

使用此类可以像这样序列化

// available products is your output
Dictionary<string, List<Products>> availableProducts = new Dictionary<string, List<Products>>();

var serializer = new JavaScriptSerializer();
dynamic result = serializer.Deserialize<dynamic>(json);

KeyValuePair<string, object> temp = ((Dictionary<string, object>)result["success"]).First();

var customerNumber = temp.Key;
var entries = (Dictionary<string, object>)temp.Value;
foreach (var kvp in entries)
{
    var products = new List<Products>();
    IEnumerable collection = (IEnumerable)kvp.Value;
    foreach (dynamic item in collection)
    {
        products.Add(new Products
        {
            ProductName = item.Key,
            Units = item.Value
        });
    }
    availableProducts[kvp.Key] = products;
}

使用的命名空间

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;