gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:56:27 25 4
gpt4 key购买 nike

我正在使用 http 请求从 asp.net 网站调用 api。该 API 以 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# 类,它不应该依赖于随每个请求而变化的时间戳?

最佳答案

这样做。

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;

关于c# - 将 JSON 数据从 Web 映射到 .NET 类,其中数据因每个 API 请求而异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28043823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com