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

Map JSON Data from the Web to .NET Class where data varies to each API request

我正在使用 http 请求从 asp.net 网站调用 api。 api returns 中的数据 JSON。

The request is something like this: GET/stats/{granularity}/{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# class,它不应该依赖于随每个请求而变化的时间戳?

//只是一个简短的片段

        //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

如果您希望您的来源也是类型化的 Class 请在此处查看我关于此类 how to convert a JSON structure to another and add a extra field in to it

的回答

这样做。

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

使用这个class你可以这样序列化

// 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;