1

I have a Json string from Visistat.com's API that I am trying to parse in C# with Json.Net. The Json string looks like:

[
    ["date", "uniques"],
    ["2014-04-15", "613"],
    ["2014-04-16", "631"],
    ["2014-04-17", "593"],
    ["2014-04-18", "466"],
    ["2014-04-19", "305"],
    ["2014-04-20", "294"],
    ["2014-04-21", "795"],
    ["2014-04-22", "666"],
    ["2014-04-23", "625"],
    ["2014-04-24", "571"],
    ["2014-04-25", "506"],
    ["2014-04-26", "342"],
    ["2014-04-27", "351"],
    ["2014-04-28", "720"],
    ["2014-04-29", "606"],
    ["2014-04-30", "588"],
    ["2014-05-01", "508"],
    ["2014-05-02", "545"],
    ["2014-05-03", "345"],
    ["2014-05-04", "379"],
    ["2014-05-05", "833"],
    ["2014-05-06", "635"],
    ["2014-05-07", "596"],
    ["2014-05-08", "530"],
    ["2014-05-09", "539"],
    ["2014-05-10", "322"],
    ["2014-05-11", "290"],
    ["2014-05-12", "734"],
    ["2014-05-13", "684"],
    ["2014-05-14", "555"],
    ["2014-05-15", "511"]
]

I've created an object to deserialize this into:

public class DateUnique
{
    public DateTime date { get; set; }
    public int uniques { get; set; }
}

I'm then trying to parse the Json string with:

List<DateUnique> dateuniques = JsonConvert.DeserializeObject<List<DateUnique>>(json);

I then get this Exception:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'VisiStatSystem.DateUnique' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '[0]', line 1, position 2.

I don't see any samples in the Json.net documentation that shows how to deserialize a Json string that looks like what is returned by the VisiStat Api. Any help appreciated!

4
  • JSON is not ordered. The parser can't assume that the first index in the array corresponds to the first object in your class. I'm not familiar with the deserializer you're using, but you may have to write your own logic to identify which is the date and which is the integer. Commented May 16, 2014 at 3:23
  • It's more like csv string rather than json string, so I would recommend you using Regex Expression instead. Commented May 16, 2014 at 4:00
  • 2
    That data is an array of arrays of strings. The fact that it's a table dump with headers is an interpretation of the data. Commented May 16, 2014 at 6:03
  • @ChrisHayes Properties in JSON objects might not be ordered, but JSON arrays ARE definitely ordered. (See JSON.org, second bullet point on the page.) Commented May 18, 2014 at 22:33

1 Answer 1

1

You are getting this error because your JSON is an array of arrays, not an array of objects, while you are trying to parse it as if it were the latter. You will need a little bit of special handling to deserialize it into a List<DateUnique>.

The following code should work:

JArray array = JArray.Parse(json);
List<DateUnique> list = new List<DateUnique>(array.Count - 1);
for (int i = 1; i < array.Count; i++)
{
    list.Add(new DateUnique
    {
        date = DateTime.ParseExact(array[i][0].ToString(), 
            "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
        uniques = int.Parse(array[i][1].ToString())
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.