0

I'm receiving the following Json response:

{"time_series_data":"[[2013-07-23T09:45:00,0.991],[2013-07-23T10:00:00,1.047],[2013-07-23T10:15:00,0.069],[2013-07-23T10:30:00,1.001],[2013-07-23T10:45:00,0.947],[2013-07-23T11:00:00,0.278],[2013-07-23T11:15:00,0.48],[2013-07-23T11:30:00,0.709],[2013-07-23T11:45:00,1.315],[2013-07-23T12:00:00,0.89],[2013-07-23T12:15:00,0.31],[2013-07-23T12:30:00,0.121],[2013-07-23T12:45:00,0.593],[2013-07-23T13:00:00,0.513],[2013-07-23T13:15:00,0.222],[2013-07-23T13:30:00,1.759],[2013-07-23T13:45:00,1.715],[2013-07-23T14:00:00,1.439],[2013-07-23T14:15:00,0.448],[2013-07-23T14:30:00,0.105]]"}

How would I read these dates and doubles into a list?

I've tried using Json.net, but I can't quite determine what the value collection above is called. Using the following code, I can pull out the value between the [[ and ]] brackets, but I'm not sure how to proceed from here.

JsonTextReader jR = new JsonTextReader(new StringReader(WebApiURL));

string data = "";

while (jR.Read())
{
    if (jR.Value != null && jR.Value != "time_series_data")
    data = jR.Value.ToString();
}

I can work with Json.net or native c#. Suggestions?

4
  • That isn't valid JSON. 2013-07-23T14:15:00 needs to be quoted. Commented Jul 23, 2013 at 21:59
  • @Blender: Hmmm...my understanding is that quotes on strings are one of the variable areas of json. Sometimes they are used and sometimes not. Regardless, the JSON above checks out as valid at jsonlint.com Commented Jul 23, 2013 at 22:31
  • Sorry, I didn't notice the quotes around the entire value of time_series_data. Double quotes in JSON are required around strings. Commented Jul 23, 2013 at 22:38
  • For future reference, quotes around strings are necessary for Json libraries to type/parse Json results correctly. Commented Jul 24, 2013 at 17:03

2 Answers 2

1

Although the JSON response you are getting is valid JSON as a whole, it isn't in a form that would allow you to extract the dates and decimals easily with a standard JSON library.

The first problem is that the "array" part is actually inside a string value. You might think that you could simply take the string value and try to re-parse it as JSON, but then you will hit another problem: the string value by itself is not valid JSON array, as @Blender noticed. In order for it to be valid, the date values would need to be quoted, since JSON does not have a native date representation.

So, one way or another, you will have to do some manual processing on the data to extract the values. Fortunately, this is not very difficult using regular expressions to split up the string. Here's the approach I would take:

First, define a simple class to hold your data items.

class DataItem
{
    public DateTime Date { get; set; }
    public double Number { get; set; }
}

Then, picking up where you left off, you can parse apart your data string like this:

List<DataItem> timeSeriesData = new List<DataItem>();
string[] pairs = Regex.Split(data, @"\[\[|\],\[|\]\]");
foreach (string pair in pairs)
{
    if (!string.IsNullOrEmpty(pair) && char.IsDigit(pair[0]))   // sanity check
    {
        string[] parts = pair.Split(',');
        DateTime date = DateTime.Parse(parts[0], CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
        double number = double.Parse(parts[1]);
        timeSeriesData.Add(new DataItem { Date = date, Number = number });
    }
}

Now you can use the timeSeriesData list as you see fit.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, I learned a few things here: 1. How to describe the second part of my data (non-standard Json); 2. More about how to form valid Json; 3. How to parse non-standard Json (Regex was the step I couldn't get my head around; and 4. How to build a data class correctly (thanks for the example). So much packed into one answer. Thanks for your thoroughness!
Incidentally, The DateTime.Parse line did not parse my dates of format 2013-07-23T09:45:00 correctly. I received a conversion error. I built my own method to parse out the date parts and rebuild a datetime, but is there a built-in method to parse this date format in c#?
Hmmmm, before I posted my answer I tested the code with the example JSON you had in your question and all the dates parsed without error for me. I am not sure why it would not be working for you. The documentation for DateTime.Parse has examples of parsing dates in the same form as you have in your data.
1

You can use http://json2csharp.com/ to get the structure of objects for VALID json and you can use Json.net library to parse them.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.