Processing JS String Object in C #
I can return a strict Javascript object to my C # project. The line looks like this:
{"QuestionID": "," QuestionTitle ":" Hiu "," OriginalURL ":", "OriginalTitle": "," ChronicID ":" "}
How can I easily convert this to an object with these parameters in C #?
UPDATE: I got it working. See code below.
SearchQuery search = (SearchQuery)JsonConvert.DeserializeObject(@filterParams, typeof(SearchQuery));
+3
source to share
3 answers
Using Json.Net
dynamic dynObj = JsonConvert.DeserializeObject(jsonstr);
Console.WriteLine("{0} {1}", dynObj.QuestionId, dynObj.QuestionTitle);
using JavaScriptSerializer
JavaScriptSerializer serializer = new JavaScriptSerializer();
var obj = serializer.Deserialize<Dictionary<string,object>>(jsonstr);
Console.WriteLine("{0} {1}", obj["QuestionId"], obj["QuestionTitle"]);
EDIT
string jsonstr = @"{""QuestionId"":""123"",""QuestionTitle"":""hiu"",""OriginalURL"":"""",""OriginalTitle"":"""",""ChronicID"":""""}";
+8
source to share
You need a JSON library for .NET. JSON stands for J ava S cript O bject N and that's basically what you pasted into your question.
I personally like Json.NET .
FYI, the "prettier" way to render the object from your question:
{ QuestionId: '', QuestionTitle: 'hiu', OriginalURL: '', OriginalTitle: '', ChronicID: '' }
+2
source to share