利用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; }
};
}
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)的更多相关文章
- java对象转为json字符串
1.使用fastjson开源json工具类库 2.java类未添加get()和set()方法,java对象初始化时,使用fastjson解析,得到的json字符串有时为空{} 3.Java对象转为js ...
- fastjson对象转为json字符串日期格式变为时间戳问题
今天尝试将map集合转为json对象时遇到一个问题.map中的value为日期格式如"2019-03-01",在使用JSONObject.toJSON(map).toString( ...
- Razor 将C#对象转换成Javascript对象, json还原被转码的字符 "·· HTML转义符
Razor 将C#对象转换成Javascript对象 在Razor中使用Json字符串,特殊字符被自动转义(如:\"->") @{ var jsonStr = Html.Ra ...
- Jackson-将对象转为Json字符串
SpringMVC-处理JSON 1.引入jackson依赖 <properties> <jackson.version>1.9.13</jackson.version& ...
- FastJSON将Java对象转为json,日期显示时间戳未格式化解决办法
JSON版本:FastJson Java 对象转换为 JSON 格式 定义以下 Person JavaBean: public class Person { @JSONField(name = &qu ...
- jackson简单使用,对象转json,json转对象,json转list
添加jackson依赖: // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core compile g ...
- ajax使用json数据格式--无效的 JSON 基元
ajax使用json数据格式提交 一开始这么写的 var flobj = { UserId: userid, ForbidSDT: ForbidSDT, ForbidEDT: ForbidEDT } ...
- JSON数组对象和JSON字符串的转化,map和JSON对象之间的转化
这种用法包括前端和后端: 前端: 1. 转化为JSON对象方便操作 var jsonObj = JSON.parse(str); 得到的是一个json数组对象,可以通过 for (var p in j ...
- 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为例 ...
随机推荐
- windows 系统文件 —— 特殊文件及文件类型
0. .mht 文件(MHTML) MHTML文件又称为聚合 HTML 文档.Web 档案或单一文件网页(聚合成单一文件).单个文件网页可将网站的所有元素(包括文本和图形)都保存到单个文件中.这种封装 ...
- Emgu-WPF学习使用-中值模糊
原文:Emgu-WPF学习使用-中值模糊 实现效果: 实现途径: 前提:Image File-> System.Drawing.Bitmap->Image<Bgr, byte> ...
- WPF中的图像处理简介
原文:WPF中的图像处理简介 和Winform中的GDI+相比,WPF提供了一组新的API用于显示和编辑图像.新API特点如下: 适用于新的或专用图像格式的扩展性模型. 对包括位图 (BMP).联合图 ...
- mvn 命令在command prompt无法识别
Download maven from this website: https://maven.apache.org/download.cgi 解压binary包后放到一个位置,比如C:\apache ...
- SQLServer 复制中移除和添加发布而不初始化所有项目
原文:SQLServer 复制中移除和添加发布而不初始化所有项目 -- 若提前"禁止架构更改",新增的列不会自动添加大发布,此时应使用 sp_articlecolumn 添加列 E ...
- C#破解access数据库密码方法
原文:C#破解access数据库密码方法 using System; using System.Collections.Generic; using System.IO; using System.L ...
- 算法之--字符串反转【python实现】
题目描述 给定一个字符串,要求把字符串前面的若干个字符移动到字符串的尾部,如把字符串“abcdef”前面的2个字符'a'和'b'移动到字符串的尾部,使得原字符串变成字符串“cdefab”.请写一个函数 ...
- 使用Boost的DLL库管理动态链接库(类似于Qt中的QLibrary)
Boost 1.61新增了一个DLL库,跟Qt中的QLibrary类似,提供了跨平台的动态库链接库加载.调用等功能.http://www.boost.org/users/history/version ...
- 毕设(五)ListView
ListView 控件可使用四种不同视图显示项目.通过此控件,可将项目组成带有或不带有列标头的列,并显示伴随的图标和文本. 可使用 ListView 控件将称作 ListItem 对象的列表条目组织成 ...
- delphi Stomp客户端连接 RabbitMQ(1)
最近公司想上个消息推送系统,网上搜了很多,因公司主要产品是Delphi,我选择了开源的RabbitMQ,Erlang语言开发,天生并行. 代码下载地址:delphistomp下载地址 windows上 ...