c# 生成json數據包


json數據類型,歸根到底就是一個字符串,管他里面什么格式,它就是一個字符串來的!

看一個json數據包:

{
    "touser":"OPENID",
    "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
    "url":"http://weixin.qq.com/download",
    "topcolor":"#FF0000",
    "data":
        {
            "User": {
            "value":"黃先生",
            "color":"#173177"
            },
            "Date":{
            "value":"06月07日 19時24分",
            "color":"#173177"
            },
            "CardNumber":{
            "value":"0426",
            "color":"#173177"
            },
            "Type":{
            "value":"消費",
            "color":"#173177"
            },
            "Money":{
            "value":"人民幣260.00元",
            "color":"#173177"
            },
            "DeadTime":{
            "value":"06月07日19時24分",
            "color":"#173177"
            },
            "Left":{
            "value":"6504.09",
            "color":"#173177"
            }
        }
}

你可以直接賦值一個string對象:

string json = "{\"touser\":\"OPENID\",.......}";

遇到雙引號要使用轉義“\”進行轉義。這樣弄出來的一個string對象就是一個json數據包了。

這樣直接賦值麻煩,在網上找了找,為了生成上面這樣的json,弄到了下面幾個類:

PayTemplateHeader.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace tenpay
{
    [DataContract] 
    public class PayTemplateHeader
    {
        public PayTemplateHeader() { }

        public PayTemplateHeader(string template_id, string touser, string url)
        {
            this.template_id = template_id;
            this.touser = touser;
            this.url = url;
        }

        /// <summary>
        /// 模板ID
        /// </summary>
        [DataMember]
        public string template_id { get; set; }

        /// <summary>
        /// 微信接收信息用戶的openid
        /// </summary>
        [DataMember]
        public string touser { get; set; }

        /// <summary>
        /// 信息點擊鏈接
        /// </summary>
        [DataMember]
        public string url { get; set; }
    }
}

PayTemplate.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace tenpay
{
    /// <summary>
    /// 微信充值模板接口請求實體對象
    /// </summary>
    [DataContract] 
    public class PayTemplate
    {
        public PayTemplate() { }

        /// <summary>
        /// 您好,您已成功進行某游戲幣充值。
        /// </summary>
        [DataMember]
        public string first { get; set; }

        /// <summary>
        /// 帳號:kantzou
        /// </summary>
        [DataMember]
        public string accountType { get; set; }

        /// <summary>
        /// 帳號:kantzou
        /// </summary>
        [DataMember]
        public string account { get; set; }

        /// <summary>
        /// 獲得游戲幣:500點
        /// </summary>
        [DataMember]
        public string productType { get; set; }

        /// <summary>
        /// 獲得游戲幣:500點
        /// </summary>
        [DataMember]
        public string number { get; set; }

        /// <summary>
        /// 充值金額:50元
        /// </summary>
        [DataMember]
        public string amount { get; set; }

        /// <summary>
        /// 充值狀態:充值成功
        /// </summary>
        [DataMember]
        public string result { get; set; }

        /// <summary>
        /// 祝您游戲愉快。
        /// </summary>
        [DataMember]
        public string remark { get; set; }
    }
}

 

JSONHelper.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace tenpay
{
    /// <summary>
    /// json轉化類
    /// </summary>
    [Serializable]
    public class JSONHelper
    {
        public static string Serialize<T>(T obj)
        {
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, obj);
            string retVal = Encoding.UTF8.GetString(ms.ToArray());
            ms.Dispose();
            return retVal;
        }

        public static T Deserialize<T>(string json)
        {
            T obj = Activator.CreateInstance<T>();
            MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
            obj = (T)serializer.ReadObject(ms);
            ms.Close();
            ms.Dispose();
            return obj;
        }
    }
}
    public class DataFormat
    {
        public string value { get; set; }
        public string color { get; set; }
    }

    public class DataFormatList
    {
        public List<DataFormat> first { get; set; }
        public List<DataFormat> accountType { get; set; }
        public List<DataFormat> account { get; set; }
        public List<DataFormat> productType { get; set; }
        public List<DataFormat> number { get; set; }
        public List<DataFormat> amount { get; set; }
        public List<DataFormat> result { get; set; }
        public List<DataFormat> remark { get; set; }
    }  
        /// <summary>
        /// 微信充值模板接口json參數整理
        /// </summary>
        /// <param name="PayTemplate">微信充值模板接口參數實例</param>
        /// <returns></returns>
        public string getPayTemplateJson(PayTemplateHeader header, PayTemplate template,string color)
        {
            string jsonH = JSONHelper.Serialize<PayTemplateHeader>(header);

            DataFormatList dataformatlist = new DataFormatList();

            dataformatlist.first = new List<DataFormat>(){  
            new DataFormat(){value=ConvertGBK(template.first),color=color}};

            dataformatlist.accountType = new List<DataFormat>(){  
            new DataFormat(){value=ConvertGBK(template.accountType),color=color}};

            dataformatlist.account = new List<DataFormat>(){  
            new DataFormat(){value=ConvertGBK(template.account),color=color}};

            dataformatlist.productType = new List<DataFormat>(){  
            new DataFormat(){value=ConvertGBK(template.productType),color=color}};

            dataformatlist.number = new List<DataFormat>(){  
            new DataFormat(){value=ConvertGBK(template.number),color=color}};

            dataformatlist.amount = new List<DataFormat>(){  
            new DataFormat(){value=ConvertGBK(template.amount),color=color}};

            dataformatlist.result = new List<DataFormat>(){  
            new DataFormat(){value=ConvertGBK(template.result),color=color}};

            dataformatlist.remark = new List<DataFormat>(){  
            new DataFormat(){value=ConvertGBK(template.remark),color=color}};

            string jsonD = new JavaScriptSerializer().Serialize(dataformatlist);

            string json = jsonH.Insert(jsonH.Length - 1, ",\"data\":" + jsonD.Replace("[", "").Replace("]", ""));

            return json;
        }

        private string ConvertGBK(string p)
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(p);
            return Encoding.GetEncoding("GBK").GetString(byteArray);
        }

調用例子:

        PayTemplateHeader header = new PayTemplateHeader();
        header.template_id = "jrciIIcHIYLJujC7FyqSiKyGGWnLok6VQJ1a81p1HLw";
        header.touser = "openid123";
        header.url = "http://www.baidu.com/App/Pay/1.aspx";

        PayTemplate paytemplate = new PayTemplate();
        paytemplate.first = "您好,您已成功進行某游戲幣充值。";
        paytemplate.accountType = "賬號";
        paytemplate.account = "k6780384";
        paytemplate.productType = "游戲幣";
        paytemplate.number = "500點";
        paytemplate.amount = "50元";
        paytemplate.result = "充值成功";
        paytemplate.remark = "祝您游戲愉快";
        TenpayUtil tenpay = new TenpayUtil();
        string post_data = tenpay.getPayTemplateJson(header, paytemplate, "#173177");
        Response.Write(post_data);

輸出:

哎,亂碼了,不管了,只要微信那邊不是亂碼就好。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM