JavaScriptSerializer implementation class

PHPz
Release: 2017-03-12 16:10:37
Original
1448 people have browsed it

I recently started writing my own projects, and finally mustered up the courage to take what I thought was a big step!

Let me share with you the general helper class first. The first one is a class that converts Object into a json sequence. There are many on the Internet, but after I tried it, most of them cannot be used, or there are various bug. In fact, there is a good class in C# that can solve this problem. It is the JavaScriptSerializer class. With this class, you can convert your object type into json and output it to the front desk with just a few lines of code. Got it!

First we need to reference the System.Web.Extensions dll in the project before we can use the JavaScriptSerializer class

The next step is the code, which is very simple.


using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Script.Serialization;using System.Data;namespace CodeHelper
{    public class JsonHelper
    {        ///  
        /// 对象转JSON 
        ///  
        /// 对象 
        /// JSON格式的字符串 
        public static string ObjectToJSON(object obj)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();            try
            {                return jss.Serialize(obj);
            }            catch (Exception ex)
            {                throw new Exception("JSONHelper.ObjectToJSON(): " + ex.Message);
            }
        }
    }
}
Copy after login

The above is the detailed content of JavaScriptSerializer implementation class. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!