The following editor will bring you an article on how to use MSScriptControl to read json data in C#. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.
There is already a JavaScriptSerializer class in C# that can deserialize json data into objects
/// <summary> /// JSON文本转对象,泛型方法 /// </summary> /// <typeparam name=”T”>类型</typeparam> /// <param name=”jsonText”>JSON文本</param> /// <returns>指定类型的对象</returns> public static T JSONToObject<T>(string jsonText) { JavaScriptSerializer jss = new JavaScriptSerializer(); try { return jss.Deserialize<T>(jsonText); } catch (Exception ex) { throw new Exception(“JSONHelper.JSONToObject(): ” + ex.Message); } }
But lazy people like me People don't want to define a class in advance. I just want to directly parse the json data passed by the client, so I'd better use MSScriptControl.ScriptControl.
With the eval method, you can do whatever you want
//Build scriptcontrol to read the json data passed by the client
MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl(); sc.Language=”JScript”; sc.AddCode(“var jsonObject=”+data );//data为提交的json文本
Then you can use eval according to the structure of json, just write it in js.
Such as
sc.Eval(“jsonObject.content.length”) sc.Eval(“jsonObject.itemValue”)
To use MSScriptControl you need toreferencecom component Microsoft Script Control 1.0.
The above is the detailed content of Detailed introduction to the method of reading json data in C# using MSScriptControl. For more information, please follow other related articles on the PHP Chinese website!