Max Wahlgren
Max Wahlgren

Reputation: 3

Converting JSON to c# from the web

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);
}

Above is my code I am attempting to retrieve the date and number of games on that date. I have achieved this with the NBA's team json data but this one is formatted differently.

public class NumGameByDate
{
    public _internal _internal { get; set; }
    public string startDate { get; set; }
    public string endDate { get; set; }
    public string startDateCurrentSeason { get; set; }
}

This is my NumGameByDate class how would you suggest storing the dates and number of games. Below is an example of how the JSON looks.

{
"_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,

Upvotes: 0

Views: 72

Answers (3)

EdSF
EdSF

Reputation: 12341

Given all the comments, particularly one that skips deserializing to some POCO, you could do something like this:

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

Output:

10/17/2017 = 2
10/18/2017 = 11
10/19/2017 = 3
10/20/2017 = 10
10/21/2017 = 11
10/22/2017 = 3
10/23/2017 = 8
10/24/2017 = 6
10/25/2017 = 10

Improve as needed/necessary. Hth ~

Upvotes: 0

Cetin Basoz
Cetin Basoz

Reputation: 23797

I think you don't need the _internal part at all (if you do you could still parse with your rawJson and class). Then you could do something like this:

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);
}

This would skip the _internal part and parse the rest as a Dictionary (although you might get as Dictionary I prefer string, string).

Upvotes: 1

Fixation
Fixation

Reputation: 989

Well, maybe javascript is better equipped if converting the file once by hand would suffice. Chances are you might need a more automatic approach.

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

This would result in the following JSON where the array is nicely populated:

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}]
}

Upvotes: 0

Related Questions