using (var webClient = new WebClient())
{
string rawJSON = webClient.DownloadString("http://data.nba.net/data/10s/prod/v1/calendar.json");
var jsonConverted = JsonConvert.DeserializeObject<NumGameByDate>(rawJSON);
}
以上是我的代码,我试图检索日期和该日期的游戏数。我已经通过NBA球队的json数据实现了这一目标,但是这一数据的格式不同。
public class NumGameByDate
{
public _internal _internal { get; set; }
public string startDate { get; set; }
public string endDate { get; set; }
public string startDateCurrentSeason { get; set; }
}
这是我的NumGameByDate类,您如何建议存储日期和游戏数。以下是JSON外观的示例。
{
"_internal": {
"pubDateTime": "2018-08-10 16:57:34.402",
"xslt": "xsl/league/schedule/marty_game_calendar.xsl",
"eventName": "_SPECIAL_ELA_EVENT_martyGameCalendar"
},
"startDate": "20171017",
"endDate": "20190410",
"startDateCurrentSeason": "20180702",
"20171017": 2,
"20171018": 11,
"20171019": 3,
"20171020": 10,
"20171021": 11,
"20171022": 3,
"20171023": 8,
"20171024": 6,
"20171025": 10,
答案 0 :(得分:1)
我认为您根本不需要_internal部分(如果您这样做,仍然可以使用rawJson和class进行解析)。然后您可以执行以下操作:
Dictionary<string,string> myData;
using (var webClient = new WebClient())
{
string rawJSON = webClient.DownloadString("http://data.nba.net/data/10s/prod/v1/calendar.json");
string myJSON = "{" + rawJSON.Substring(rawJSON.IndexOf(@"""startDate"));
myData = JsonConvert.DeserializeObject<Dictionary<string,string>>(myJSON);
}
这将跳过_internal部分,并将其余部分解析为Dictionary(尽管您可能会得到Dictionary,但我更喜欢使用string,string)。
答案 1 :(得分:0)
好吧,如果只需一次转换文件就可以满足javascript的需求。您可能需要更自动的方法。
// Load your JSON in variable x
var x = { "someOtherProperty": { etc:true }, "20171017": 2, "20171018": 11, "20171019": 3, "20171020": 10, "20171021": 11, "20171022": 3, "20171023": 8, "20171024": 6 }
x.games = [] // create an array that will hold the converted values.
for (key in x){
// Some regex testing if the current property is a date
if (/[2][0][01][0-9][0-1][0-9][0-3][0-9]/.test(key)){
x.games.push({ date: key, gameCount: x[key] }); // Put the new object in an actual array
delete x[key] // Delete the old value
}
}
这将导致以下JSON正确填充数组:
x = {
"someOtherProperty": { etc:true },
"games":[{"date":"20171017","gameCount":2},{"date":"20171018","gameCount":11},{"date":"20171019","gameCount":3},{"date":"20171020","gameCount":10},{"date":"20171021","gameCount":11},{"date":"20171022","gameCount":3},{"date":"20171023","gameCount":8},{"date":"20171024","gameCount":6},{"date":"20171025","gameCount":10}]
}
答案 2 :(得分:0)
鉴于所有评论,特别是跳过对一些POCO进行反序列化的评论,您可以执行以下操作:
if (chkGetMedian.Checked)
{
$"{employee}: {formattedTime} - Employee Median = {currentEmployeeMedian} minutes";
}
else
{
$"{employee}: {formattedTime}";
}
输出:
//Dependency JSON.Net
var obj = JObject.Parse(your_sample_json_string);
foreach (var t in obj)
{
DateTime d;
if (DateTime.TryParseExact(t.Key, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out d))
{
Console.WriteLine("{0} = {1}", d.ToShortDateString(), t.Value);
}
}
根据需要/必要进行改进。嗯〜