I have two JSON files from two different URLs:
url1:
{"2013" : [
{ "date":"2/5/2013 11:13 AM","height":3 }],
"2012" : [
{ "date":"28/9/2012 3:34 PM","height":4 }]
}
url2:
{"2013" : [
{ "date":"2013-01-09 12:00:00","height":0 },
{ "date":"2013-01-29 12:00:00","height":2 }],
"2012" : [
{ "date":"2012-02-09 12:00:00","height":0 },
{ "date":"2012-02-29 12:00:00","height":2 }],
"2011" : [
{ "date":"2011-03-09 12:00:00","height":3 },
{ "date":"2011-03-29 12:00:00","height":2 }]
}
What I want to happen is combine them into these:
{"2013" : [
{ "date":"2013-05-02 11:13:00","height":3 },
{ "date":"2013-01-29 12:00:00","height":2 },
{ "date":"2013-01-09 12:00:00","height":0 }],
"2012" : [
{ "date":"2012-09-28 15:34:00","height":2 },
{ "date":"2012-02-29 12:00:00","height":2 },
{ "date":"2012-02-09 12:00:00","height":0 }],
"2011" : [
{ "date":"2011-03-29 12:00:00","height":2 },
{ "date":"2011-03-19 12:00:00","height":8 },
{ "date":"2011-03-09 12:00:00","height":3 }]
}
Currently, what I am trying to do is two separate WebClient downloads, then attempting to combine them afterwards (unsuccessfully). I later realized that doing two separate WebClient downloads, one might not be finished before I attempt to combine them, resulting in a null reference.
WebClient Url1 = new WebClient();
Url1.DownloadStringCompleted += new
DownloadStringCompletedEventHandler(Url1_DownloadStringCompleted);
Url1.DownloadStringAsync(new Uri("http://example.com"));
WebClient Url2 = new WebClient();
Url2.DownloadStringCompleted += new
DownloadStringCompletedEventHandler(Url2_DownloadStringCompleted);
Url2.DownloadStringAsync(new Uri("http://anotherexample.com"));
void Url1_DownloadStringCompleted(object sender,
DownloadStringCompletedEventArgs e)
{
if (e.Error != null) return;
json1 = JObject.Parse(e.Result);
}
void Url2_DownloadStringCompleted(object sender,
DownloadStringCompletedEventArgs e)
{
if (e.Error != null) return;
json2 = JObject.Parse(e.Result);
}
What I've read so far is doing a queue, then combining it. However, I'm not sure if it makes sense as it will make the combined file an invalid JSON file, because of mismatching brackets. Also, I would have problems with regards to parsing dates because of the different formats.
Any suggestions as to what flow should I go?
As @cgatian said, you should create new class:
public class Item
{
public DateTime Date { get; set; }
public int Height { get; set; }
}
And then:
var dictionary1 = JsonConvert.DeserializeObject<Dictionary<int, Item[]>>(jsonFromUrl1);
var dictionary2 = JsonConvert.DeserializeObject<Dictionary<int, Item[]>>(jsonFromUrl2);
var mergedDictionary = dictionary1
.Concat(dictionary2)
.GroupBy(x => x.Key, y => y.Value)
.ToDictionary(
group => group.Key,
group => group.SelectMany(x => x).ToArray());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With