zoukankan      html  css  js  c++  java
  • 利用jsonconvert来转换json数据格式 (对象转为json)

     

    今天学了一下.net的WCF组件,边心血来潮,想着现在不都是前后分离,调接口开发不,于是赶紧写了一简单的后台数据,哈哈  废话不多说,直接上代码;

    注意需要导入库!

    实体类:Customer

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace domain
    {
        public class Customer
        {
            public string CustomerId { set; get; }
            public string CompanyName { set; get; }
            public string ContactName { set; get; }
            public string Address { set; get; }
            public string test1 { set; get; }
        };
    }
    View Code

    WCF接口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    
    namespace 接口测试学习1
    {
        // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
        [ServiceContract]
        public interface IService1
        {
    
            [OperationContract]
            string GetData(int value);
    
            [OperationContract]
            CompositeType GetDataUsingDataContract(CompositeType composite);
    
            // TODO: 在此添加您的服务操作
            [OperationContract]
            string GetDataJson(string customer);
        }
    
    
        // 使用下面示例中说明的数据约定将复合类型添加到服务操作。
        [DataContract]
        public class CompositeType
        {
            bool boolValue = true;
            string stringValue = "Hello ";
    
          [DataMember]
            public bool BoolValue
            {
                get { return boolValue; }
                set { boolValue = value; }
            }
    
              [DataMember]
            public string StringValue
            {
                get { return stringValue; }
                set { stringValue = value; }
            }
    
          
        }
    }
    View Code

    WCF接口的实现类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    using System.Data.SqlClient;
    using domain;
    using Newtonsoft.Json;
    namespace 接口测试学习1
    {
        // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
        // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs,然后开始调试。
        public class Service1 : IService1
        {
           
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
    
            public CompositeType GetDataUsingDataContract(CompositeType composite)
            {
                if (composite == null)
                {
                    throw new ArgumentNullException("composite");
                }
                if (composite.BoolValue)
                {
                    composite.StringValue += "Suffix";
                }
                return composite;
            }
    
    
            public string GetDataJson(string customer)
            {
    
                string strcon = "Data Source=192.168.99.28;Initial Catalog=EDU;User Id=Pos;Password=Pos;";
                SqlConnection con = new SqlConnection(strcon);
                List<Customer> list = new List<Customer>();
                try
                {
                   
                    con.Open();
                    //将执行的sql
                 //   string sql = "select * from Customer";
                    string sql = string.Format("select * from {0}", customer);
                    //创建命令对象,指定要执行sql语句与连接对象conn
                    SqlCommand cmd = new SqlCommand(sql, con);
    
                    Console.WriteLine("打开成功,状态" + con.State);
                    //执行查询返回结果集
                    SqlDataReader sdr = cmd.ExecuteReader();
                    while (sdr.Read())
                    {
                        Customer c = new Customer();
                        c.CustomerId = sdr["CustomerId"].ToString();
                        c.CompanyName = sdr["CompanyName"].ToString();
                        c.ContactName = sdr["ContactName"].ToString();
                        c.Address = sdr["Address"].ToString();
                        c.test1 = sdr["test1"].ToString();
    
                        list.Add(c);
                    }
    
                } catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally {
                //关闭连接
                con.Close();
               // Console.WriteLine("总共查询了" + count +"条数据");
             //   Console.ReadKey();
                Console.WriteLine(list.Capacity);
                }
                //再把list集合进行序列化,转json
                string json = JsonConvert.SerializeObject(list);
                Console.WriteLine(json);
                Console.ReadKey();
                return json;
               
            }
               
             
        }
            
        
    }
    View Code
  • 相关阅读:
    [OpenGL ES 071]光照原理
    [OpenGL ES 03]3D变换:模型,视图,投影与Viewport
    [日志]当今最流行的网络生僻字,很火
    [日志]关于茶的基础知识
    [健康]快速除牙痛的八个小验方
    [日志]我们生活中的潜规则
    [日志]做事要方,做人要圆
    [日志]家居装修花钱看你怎么省
    [日志]非常宝贵的工作经验
    [日志]你用的着的一些家装尺寸数据
  • 原文地址:https://www.cnblogs.com/cb1186512739/p/9504887.html
Copyright © 2011-2022 走看看