已知json类型根据类型封装集合

1编写帮助类根绝url得到json

public static string Post(string url)
        {
            string strURL = url;

            //创建一个HTTP请求  
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            //Post请求方式  
            request.Method = "POST";
            //内容类型
            request.ContentType = "json";

            //设置参数,并进行URL编码  
            //虽然我们需要传递给服务器端的实际参数是JsonParas(格式:[{\"UserID\":\"0206001\",\"UserName\":\"ceshi\"}]),
            //但是需要将该字符串参数构造成键值对的形式(注:"paramaters=[{\"UserID\":\"0206001\",\"UserName\":\"ceshi\"}]"),
            //其中键paramaters为WebService接口函数的参数名,值为经过序列化的Json数据字符串
            //最后将字符串参数进行Url编码
            string paraUrlCoded = System.Web.HttpUtility.UrlEncode("paramaters");
            paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode("");

            byte[] payload;
            //将Json字符串转化为字节  
            payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
            //设置请求的ContentLength   
            request.ContentLength = payload.Length;
            //发送请求,获得请求流  

            Stream writer;
            try
            {
                writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
            }
            catch (Exception)
            {
                writer = null;
                Console.Write("连接服务器失败!");
            }
            //将请求参数写入流
            writer.Write(payload, 0, payload.Length);
            writer.Close();//关闭请求流

            String strValue = "";//strValue为http响应所返回的字符流
            HttpWebResponse response;
            try
            {
                //获得响应流
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
            }

            Stream s = response.GetResponseStream();

            //服务器端返回的是一个XML格式的字符串,XML的Content才是我们所需要的Json数据

            StreamReader Reader = new StreamReader(s);

            strValue = Reader.ReadToEnd();//取出Content中的Json数据
            Reader.Close();
            s.Close();

            return strValue;//返回Json数据
        }

  2设定已知json类型

 public static dynamic GetJsonType(this int jsonType)
        {
            if ((int)JsonType.订单 == jsonType)
            {
                return new
                {
                    response = new
                    {
                        orderList = new
                        {
                            order = new[] { 
                    new { 
                        orderId = "", 
                        orderCode = "",
                        orderStatus = "",
                        orderAmount = "", 
                        productAmount = "", 
                        orderCreateTime = "", 
                        orderDeliveryFee = "", 
                        orderNeedInvoice = "", 
                        updateTime = "",
                        endUserId = "",
                        warehouseId = "", } }
                        },
                        totalCount = 0,
                        errorCount = 0
                    }
                };
            }
            if ((int)JsonType.订单详情 == jsonType)
            {
                return new
                {
                    response = new
                    {
                        orderInfo = new
                        {
                            orderDetail = new
                            {
                                orderId = "",
                                orderCode = "",
                                orderStatus = "",
                                orderAmount = "",
                                productAmount = "",
                                realAmount = "",
                                orderCreateTime = "",
                                orderDeliveryFee = "",
                                orderNeedInvoice = "",
                                goodReceiverName = "",
                                goodReceiverAddress = "",
                                goodReceiverProvince = "",
                                goodReceiverCity = "",
                                goodReceiverCounty = "",
                                goodReceiverPostCode = "",
                                goodReceiverPhone = "",
                                goodReceiverMoblie = "",
                                orderPaymentConfirmDate = "",
                                payServiceType = "",
                                orderPromotionDiscount = "",
                                deliverySupplierId = "",
                                merchantRemark = "",
                                updateTime = "",
                                orderCouponDiscount = "",
                                orderPlatformDiscount = "",
                                endUserId = "",
                                warehouseId = "",
                            }
                        },
                        orderItemList = new
                        {
                            orderItem = new[] 
                            { 
                                new
                                {
                                    id="",
                                    orderId="",
                                    orderItemAmount = "",
                                    orderItemNum = "",
                                    orderItemPrice = "",
                                    productCName = "",
                                    productId = "",
                                    originalPrice = "",
                                    merchantId = "",
                                    updateTime = "",
                                    groupFlag = "",
                                    deliveryFeeAmount = "",
                                    promotionAmount = "",
                                    couponAmountMerchant = "",
                                    couponPlatformDiscount = "",
                                    subsidyAmount = "",
                                }
                            }
                        }
                    }
                };
            }
            return null;
        }

3调用

var text = GetJson.Post("http://localhost:7822/SaleOrders/Read");
            var type = ((int)JsonType.订单).GetJsonType();
            var obj = JsonConvert.DeserializeAnonymousType(text, type);

上面的结果”obj“就是我们根据json的结构封装的匿名对象

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。