Home > Backend Development > C#.Net Tutorial > Detailed introduction to the method of reading json data in C# using MSScriptControl

Detailed introduction to the method of reading json data in C# using MSScriptControl

黄舟
Release: 2017-03-23 13:18:01
Original
2238 people have browsed it

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);
}
}
Copy after login

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文本
Copy after login

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”)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template