JSON feed first DataContract empty

Your JSON feed will probably look similar to this:

[{"employees": [{ "firstName":"John" , "lastName":"Doe" },{ "firstName":"Anna" , "lastName":"Smith" },{ "firstName":"Peter" , "lastName":"Jones" }]}]

As you can see, the feed is starting with square brackets.
But when deserializing the feed you don’t get any data.
It’s because the first DataContract is empty / null.
Hereby you can’t link/map the DataMembers and won’t get any data from the feed.

Well, this can be fixed with a little workaround:

WebClient wc = new WebClient();
byte[] json = wc.DownloadData("file.json");

String newJson = Encoding.UTF8.GetString(json);
newJson = newJson.Remove(0, 1);
newJson = newJson.Remove(newJson.Length - 1, 1);

using (MemoryStream ms = 
new MemoryStream(Encoding.UTF8.GetBytes(newJson)))

Comments are closed.

%d bloggers like this: