今天学了一下.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; }
};
}

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; }
} }
}

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; } } }

利用jsonconvert来转换json数据格式 (对象转为json)的更多相关文章

  1. java对象转为json字符串

    1.使用fastjson开源json工具类库 2.java类未添加get()和set()方法,java对象初始化时,使用fastjson解析,得到的json字符串有时为空{} 3.Java对象转为js ...

  2. fastjson对象转为json字符串日期格式变为时间戳问题

    今天尝试将map集合转为json对象时遇到一个问题.map中的value为日期格式如"2019-03-01",在使用JSONObject.toJSON(map).toString( ...

  3. Razor 将C#对象转换成Javascript对象, json还原被转码的字符 &quot·· HTML转义符

    Razor 将C#对象转换成Javascript对象 在Razor中使用Json字符串,特殊字符被自动转义(如:\"->") @{ var jsonStr = Html.Ra ...

  4. Jackson-将对象转为Json字符串

    SpringMVC-处理JSON 1.引入jackson依赖 <properties> <jackson.version>1.9.13</jackson.version& ...

  5. FastJSON将Java对象转为json,日期显示时间戳未格式化解决办法

    JSON版本:FastJson Java 对象转换为 JSON 格式 定义以下 Person JavaBean: public class Person { @JSONField(name = &qu ...

  6. jackson简单使用,对象转json,json转对象,json转list

    添加jackson依赖: // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core compile g ...

  7. ajax使用json数据格式--无效的 JSON 基元

    ajax使用json数据格式提交 一开始这么写的 var flobj = { UserId: userid, ForbidSDT: ForbidSDT, ForbidEDT: ForbidEDT } ...

  8. JSON数组对象和JSON字符串的转化,map和JSON对象之间的转化

    这种用法包括前端和后端: 前端: 1. 转化为JSON对象方便操作 var jsonObj = JSON.parse(str); 得到的是一个json数组对象,可以通过 for (var p in j ...

  9. json数据格式 net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of Error:(findColumns1)Read timed out

    substring(3)的用法http://www.w3school.com.cn/jsref/jsref_substring.asp 可能一:sb是要转化的数据,以sb是String为例       ...

随机推荐

  1. TargetNullValue与FallbackValue

    原文:TargetNullValue与FallbackValue TargetNullValue与FallbackValue都是BindingBase的属性. TargetNullValue:获取或设 ...

  2. ASP .NET Views文件夹下面的文件找不到

    习惯将页面和它对应的js,css文件放在一个文件夹下,将这些都放在Views文件夹下     运行的时候发现找不到js和css文件 因为在MVC中,是不建议直接去访问Views文件夹的我们建立的ASP ...

  3. WPF小笔记-Popup拖动

    查看了下MSDN发现Popup没有类拟Drag相关的属性和方法,第一时间想了thumb.忙了一会未果,就想起了强大的google. 发现中文资料很少,英文的发现有两篇很不错的,所以笔记在博客园里,希望 ...

  4. Z-Order

    The z-order of a window indicates the window's position in a stack of overlapping windows. This wind ...

  5. 字符串、数组操作函数 Copy Concat Delete Insert High MidStr Pos SetLength StrPCopy TrimLeft

    对字符串及数组的操作,是每个程序员必须要掌握的.熟练的使用这些函数,在编程时能更加得心应手. 1.Copy 功能说明:该函数用于从字符串中复制指定范围中的字符.该函数有3个参数.第一个参数是数据源(即 ...

  6. Linux编译安装Qt 5.4.1(-qt-xcb是必须要指定的,卸载自带的gcc等)

    转载请注明文章:Linux编译安装Qt 5.4.1 出处:多客博图 很久不写文章了,过程很简单,但是操作很多,简单说吧. 前言: 操作系统CentOS 6.6,64位的. 1.安装gcc 4.8.4, ...

  7. Asp +Js 无刷新分页

    Default.aspx代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=" ...

  8. Mac App Store应用签名和pkg签名(必须签名后才能销售)

    App签名 只有用苹果颁发的证书签名的应用才能在App Store上进行销售,所以我们开发的应用必须打上签名. 签名有两种方式,一是使用Xcode,在配置里面设置签名,编译出来的app就有了签名:二是 ...

  9. UWP入门(二) -- 基础笔记

    原文:UWP入门(二) -- 基础笔记 不错的UWP入门视频,1092417123,欢迎交流 UWP-04 - What i XMAL? XAML - XML Syntax(语法) ,create i ...

  10. Fundamentals Code Library,包含HTTP TCP JSON BigInteger 加密算法 Unicode等许多东西

    http://fundementals.sourceforge.net/index.html https://github.com/fundamentalslib/fundamentals5 http ...