0

I am receiving some JSON data which is not in a "name:value" pair but its something like this:

[[\"ManagerID\",\"EmployeeID\",\"Domain\"],[\"2\",\"110\",\"BBU\"]]

I am not able to parse this data using JsonConvert.DeserializeObject as I get an error

"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 '', line 1, position 1."

I am using Visual Studio 2008 and .net framework 3.5. Let me know in case you need more clarification.

2
  • The error message says all. How does your object look like? Commented Feb 19, 2014 at 11:53
  • Well I am receiving the above data in a string as it is the way returned by the web service. So what I want to do is parse the above mentioned string and store it in a List. Commented Feb 19, 2014 at 12:00

1 Answer 1

2

Lets assume your object is of this class:

class MyObject
{
    public int ManagerID { get; set; }
    public int EmployeeID { get; set; }
    public string Domain { get; set, }
}

Then your JSON does not match it, your JSON should look like:

{"ManagerID": 2, "EmployeeID": 110, "Domain": "BBS"}

If you really want to read the above JSON you need:

JsonConvert.DeserializeObject<string[][]>(yourString);

Tried

Newtonsoft.Json.JsonConvert.DeserializeObject<string[][]>("[[\"ManagerID\",\"EmployeeID\",\"Domain\"],[\"2\",\"110\",\"BBU\"]]")

But that is not a handy structure...

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

5 Comments

Hey Thanks a ton! Really appreciate your quick response. Was stuck with this thing for hours. And yes the JSON is not in a correct format but that's what WebService returns and its not under my control.
Just a quick question. How do I populate the data to List<MyObject> directly?
How does the data provided by the WebService look like when it has more than one record/entry?
Something like this: [[\"ManagerID\",\"EmployeeID\",\"Domain\"],[\"2\",\"110\",\"BBU\"]],[\"5\",\"150\",\"SBU\"]] where the first row defines the ColumnNames and the rest of the rows are actual data.
I see. Unforunately you will have to do this with a small loop by yourself. Like for (int i = 1; i < array.GetLength(0); i++) { myList.Add(new MyObject() { ManagerID = Convert.ToInt32(array[i][0]), .. }); Maybe you will have to parse the column names instead if using fixed indices, maybe you have to expect conversion errors and handle them. You must know ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.