I am having an issue when read JSON returned from my ASP.NET webservice to the client. The JSON returned contains an ObjectId generated in MongoDB database, and I need it in the client.
The problem start when I run
var res = jQuery.parseJSON(data.d)[0]; // array of companies
I get the error:
Uncaught SyntaxError: Unexpected token O
I assume is that the Javascipt JSON parser doesn't recognize the ObjectId.
In my .NET C# code I seralized and return JSON:
var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
return results.ToJson(jsonWriterSettings);
The return JSON string from the webservice:
[{ "companies" : [{ "id" : ObjectId("53e129ed0000000000000000"), "name" :
"Company 1" }, { "id" : ObjectId("53e12a290000000000000000"), "name" :
"Company 2" }, { "id" : ObjectId("53e12a650000000000000000"), "name" :
"Company 3" }] }]"
How can I use the ObjectId in Javascript. I do need the string of the ObjectId in some of the HTML elements (e.g. drop down list values)?
Update: I found out about the BSON seralizer here. However, I don't know if that's the appropriate solution.