特殊的Josn格式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了特殊的Josn格式相关的知识,希望对你有一定的参考价值。

 static void Main(string[] args)
        {
            YtRequest<RequestHead, RequestBody> Ytrequest = new YtRequest<RequestHead, RequestBody>
            {
                head = new RequestHead
                {
                    version="1.0",
                    product="B10002",
                    merchant="201511043830",
                    outOrderId = "110000000000005",
                    memo="备注信息",
                },
                body = new RequestBody
                {
                    name_card="",
                    id_card="",
                },
            };

            javascriptSerializer serializer = new JavaScriptSerializer();
            var prestr = serializer.Serialize(Ytrequest);
            string mysign = Sign(prestr, "N75tkwmnHV1UjZ60YAGK", "utf-8");

            YtSendModel<RequestHead, RequestBody> Ytsend = new YtSendModel<RequestHead, RequestBody>
            {
                request = new YtRequest<RequestHead, RequestBody>
                {
                    head = new RequestHead
                    {
                        version = "1.0",
                        product = "B10002",
                        merchant = "201511043830",
                        outOrderId = "123456",
                        memo = "连连实名认证",
                    },
                    body = new RequestBody
                    {
                        name_card = "min",
                        id_card = "141002465",
                    },
                },
                sign="12321435636",
            };

            string strRequestData = serializer.Serialize(Ytsend);

            Encoding enconding = Encoding.GetEncoding("utf-8");

            byte[] bytedata = enconding.GetBytes(strRequestData);


            HttpWebRequest re = (HttpWebRequest)HttpWebRequest.Create("https://api.udcredit.com/api/credit/v1/get_nauth");
            re.Method = "post";
            re.ContentType = "application/json";

            re.ContentLength = bytedata.Length;
            Stream requestStream = re.GetRequestStream();
            requestStream.Write(bytedata, 0, bytedata.Length);
            requestStream.Close();

            HttpWebResponse httpResponse = (HttpWebResponse)re.GetResponse();
            Stream myStream = httpResponse.GetResponseStream();

            StreamReader reader = new StreamReader(myStream, enconding);
            StringBuilder responseData = new StringBuilder();

            String line;
            while ((line = reader.ReadLine()) != null)
            {
                responseData.Append(line);
            }

            //释放
            myStream.Close();

            string strResult = responseData.ToString();

      
        }

        static  string Sign(string str,string key,string charset)
        {
            str=str+key;
            var sb=new StringBuilder(32);
            MD5 md5=new MD5CryptoServiceProvider();
            var arr=md5.ComputeHash(Encoding.GetEncoding(charset).GetBytes(str));
            foreach (var item in arr)
            {
                sb.Append(item.ToString("x").PadLeft(2, ‘0‘));
            }
            return sb.ToString();
        }

建一个YtSendModel.cs 

public class YtSendModel<Head ,Body>
    {
       public YtRequest<Head, Body> request { get; set; }
       public string sign { get; set; }
    }
   public class YtRequest<Head, Body>
   {
       public Head head { get; set; }
       public Body body { get; set; }
   }
    public class RequestHead
    {
        public string version { get; set; }
        public string product { get; set; }
        public string merchant { get; set; }
        public string outOrderId { get; set; }
        public string memo { get; set; }
    }
    public class RequestBody
    {
        public string name_card { get; set; }
        public string id_card { get; set; }
    }

 

以上是关于特殊的Josn格式的主要内容,如果未能解决你的问题,请参考以下文章

Velocity——velocity遍历josn格式的字符串

如何将多个数据的- 转为:来匹配josn格式

JOSN

java中如何将list按照键值的格式转换为josn

JS中对JOSN的解析

Java中 用ajax发出请求返回JOSN格式数据,中文乱码问题