我可以将字符串化的Javascript对象返回到C#项目。该字符串如下所示:



如何在C#中使用这些参数轻松地将其转换为对象?

更新:我知道了。请参见下面的代码。

SearchQuery search = (SearchQuery)JsonConvert.DeserializeObject(@filterParams, typeof(SearchQuery));

最佳答案

使用Json.Net

dynamic dynObj = JsonConvert.DeserializeObject(jsonstr);
Console.WriteLine("{0} {1}", dynObj.QuestionId, dynObj.QuestionTitle);

使用JavaScriptSerializer
JavaScriptSerializer serializer = new JavaScriptSerializer();
var obj = serializer.Deserialize<Dictionary<string,object>>(jsonstr);
Console.WriteLine("{0} {1}", obj["QuestionId"], obj["QuestionTitle"]);

编辑
string jsonstr = @"{""QuestionId"":""123"",""QuestionTitle"":""hiu"",""OriginalURL"":"""",""OriginalTitle"":"""",""ChronicID"":""""}";

09-25 22:25