Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

OK, I'm totally new to JSON and Json.NET - so please forgive my ignorance;

I'm trying to parse a string which comes from a GET request into something useful for me in a C# application.

The string contains a structure with nested JSON objects. The objects on the first level has UTF-8 encodings like \u0040 and escape characters like \. The next level contains another nested JSON object with double escape characters like \\ and so on (down to 5 escape characters!)..

It also contains " representations for quotation marks like ".

My first assumtion is that these are nested JSON objects. How can I parse these nested objects into something useful for me to query with, for instance, LINQ? Do I have to write the logic my self, or is there a nice function for this in Json.NET already? The first bytes of the string to help clarify:

{\"phase\":2,\"id\":\"pagelet_search_results\",\"is_last\":true,\"css\":[\"Jo2rQ\",\"pxy5B\"],\"js\":[\"tVaAM\",\"rLVa6\",\"FJ3LF\"],\"resource_map\":{\"FJ3LF\":{\"type\":\"js\",\"src\":\"http:\\/\\/e.static.ak.fbcdn.net\\/rsrc.php\\/y8\\/p\\/r\\/1NVEOfjbXp5.js\"},\"pxy5B\":{\"type\":\"css\",\"permanent\":1,\"src\":\"http:\\/\\/f.static.ak.fbcdn.net\\/rsrc.php\\/yc\\/r\\/9H-KBGVNlw_.css\"}},\"onload\":[\"window.__UIControllerRegistry[\\\"c4d4ab726887b68c58602753\\\"] = new UIPagelet(\\\"c4d4ab726887b68c58602753\\\", \\\"\\\\\\/pagelet\\\\\\/generic.php\\\\\\/SearchObjectResultsPagelet\\\\\\/\\\", {\\\"params\\\":{\\\"viewerContext\\\":{\\\"userID\\\":123454549,\\\"accountID\\\":123454549,\\\"appID\\\":0,\\\"isOmnipotent\\\":false,\\\"isAuthenticated\\\":true,\\\"accessTokens\\\":[],\\\"instanceKey\\\":\\\"1254318719\\\\\\/1256318759\\\\\\/0\\\",\\\"originalViewerContext\\\":null,\\\"__index\\\":5,\\\"__sampleId\\\":null,\\\"__next\\\":-808,\\\"__state\\\":4,\\\"__preparer\\\":{\\\"runnablePreparables\\\":[],\\\"newRunnablePreparables\\\":[],\\\"blockedPreparables\\\":[],\\\"isRunning\\\":false,\\\"current\\\":null,\\\"isSequential\\\":false,\\\"round\\\":4,\\\"index\\\":3},\\\"__addedPreparers\\\":{\\\"3\\\":{\\\"runnablePreparables\\\":[],\\\"newRunnablePreparables\\\":[],\\\"blockedPreparables\\\":[],\\\"isRunning\\\":false,\\\"current\\\":null,\\\"isSequential\\\":false,\\\"round\\\":4,\\\"index\\\":3}},\\\"__siblings\\\":[],\\\"__children\\\":[],\\\"__ancestors\\\":[],\\

I appreciate your help, folks ;)

share|improve this question
FYI there is a fully functional JSON serializer/deserializer in the FCL. msdn.microsoft.com/en-us/library/… – James Gaunt Feb 7 '11 at 18:48

2 Answers

If you know the contract of the object you got, create a C# POCO class/classes with the same contracts and deserialize your JSON with JsonSerializer.Deserialize. If the contract is unknown, try JObject.Parse(jsonString) and methods like Values() and PropertyValues will return you collection you can query with LINQ.

share|improve this answer
using Newtonsoft.Json;
using Newtonsoft.Json.Ling;

.....


string js = "{\"phase\":2,\"id\":\"pagelet_search_results\"}";    
JObject jo = JObject.Parse(jsonString);
int phase = (int)jo.SelectToken("phase");
string id = (string)jo.SelectToken("id");
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.