I've got the following
[
{
"name": "ATVI",
"dailyClosePrice": [
{
"3/15/2017": 210.08,
"4/6/2017": 235.08,
"4/21/2017": 243.61,
"2/16/2017": 205.43
}
]
},
{
"name": "ADBE",
"dailyClosePrice": [
{
"3/15/2017": 241.96,
"4/6/2017": 270.22,
"4/21/2017": 281.22,
"2/16/2017": 225.26
}
]
}
]
The actual file has many more than 2, but you can see there are two of
the main object here.
I'm having trouble parsing the key-value pairs such as "3/15/2017": 210.08
.
I need to make a class out of this, I guess an example class would have
class Stock
{
public string Name{get;set;}
public List<Entry> Prices{get;set;}
}
class Entry
{
public string Date{get;set;}
public double Price{get;set;}
}
So I'm trying to parse the file into a List
I guess I could use a StreamReader and use the Seek method to find a ":", but what I am having trouble with is then how to be able to only read in "ATVI". Once I get "ATVI" for example I set that string to a Name in a Stock, then can Seek for "{" and read in "3/15/2017" and set that to a Date in an Entry then Seek to a ":" and read in the double, etc.
If I do the above, I don't know how to read in select elements of the stream, in C#. In C# I mostly have read in strings that have a common spacing, usually I would use C for more complex things like here, but I am really hoping to use c# instead of C for this.
I don't care what method I need to use, I just want to read in the file.
If I had something like below, I would just use NewtonSoft
[
{
"name": "ATVI",
"dailyClosePrice": [
{"Date":"3/15/2017", "price":210.08},
{"Date":"3/15/2017", "price": 210.08},
{"Date":"4/6/2017", "price": 235.08},
{"Date":"4/21/2017", "price": 243.61}
]
},
{
"name": "ADBE",
"dailyClosePrice": [
{"Date":"3/15/2017", "price":210.08},
{"Date":"3/15/2017", "price": 210.08},
{"Date":"4/6/2017", "price": 235.08},
{"Date":"4/21/2017", "price": 243.61}
]
}
]
You definitely shouldn't be parsing this by hand - Json.NET will be absolutely fine with this, so long as you model it correctly. The JSON represents a list of objects, each of which has a name
(string) and a dailyClosePrice
which appears to be a list which always has a single value, which is in turn an object mapping dates to prices. You can represent that fairly easily:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
public class Stock
{
public string Name { get; set; }
public List<Dictionary<string, decimal>> DailyClosePrice { get; set; }
}
class Test
{
static void Main(string[] args)
{
string json = File.ReadAllText("test.json");
var stocks = JsonConvert.DeserializeObject<List<Stock>>(json);
foreach (var stock in stocks)
{
Console.WriteLine($"Name: {stock.Name}");
Console.WriteLine("Prices:");
// Assume there's only ever a single entry, at least for now...
var prices = stock.DailyClosePrice.Single();
// TODO: Parse the (ugly, US-based) date format...
var entries = prices.Select(kvp => new { Date = kvp.Key, Price = kvp.Value });
foreach (var entry in entries)
{
Console.WriteLine($" {entry.Date}: {entry.Price}");
}
}
}
}
Now that Stock
class is pretty ugly, but it represents what's in the JSON - it should then be easy enough to convert that representation into a more palatable one for the rest of your code.
See more on this question at Stackoverflow