当前位置: 移动技术网 > IT编程>开发语言>.net > JSON在ASP.NET中使用方法

JSON在ASP.NET中使用方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

赵敏书,军情观察,富士山下钢琴谱

json.net的简单介绍
首先介绍一个为方便在.net中使用json的api,json.net。它方便我们读取从浏览器流向服务器的json对象,也方便在响应流中写入json对象。

json.net只提供了服务器端的方法,主要有实现json文本与xml互相转换的类,有自定义读写json的jsonreader类和jsonwriter类,还有一个非自定义读写json的javascriptserializer类。

asp.net ajax中,服务器端由javascriptserializer类的几个方法来用于实现序列化和反序列化能力。在json.net中,服务器端的序列化和反序列化能力则主要由javascriptconvert类的几个方法提供。本篇的例子只使用了javascriptconvert。

javascriptconvert
json.net中,这个类用于序列化和反序列化javascript对象。
这个类有两个方法:

  • serializeobject(object value, params jsonconverter[] converters),序列化,它有个重载方法serializeobject(object value)
  • deserializeobject(string value, type type),反序列化,它有个重载方法deserializeobject(string value)

在客户端,json.net未提供支持。

下面我们尝试用这个api在asp.net中实现用json交互数据。

使用json.net在c/s中交互json数据的简单例子
1、先新建一个asp.net 网站。

2、将下载到的binary文件夹中的newtonsoft.json.dll和newtonsoft.json.xml放入网站的bin文件,当然要先新建bin文件夹。然后对dll添加引用。

3、切换到设计模式,从标准工具箱向页面上添加三个label,text分别为employeeid、employeename、employeeinfo;三个textbox,id分别为txtid、txtname、txtinfo;然后添加一个button,id为btntojsonstring,text为invoke tojsonstring;然后添加一个textbox,id为txtjson,textmode为multiline,rows设为5;接着再分别添加一个button和textbox,id为btntoobject、txtstremployee,button的text为invoke tostremployee。

4、添加一个webservice项目。

编写一个employee类,然后两个webmethod,接着在项目中对该web服务添加引用。代码如下:

using system;
using system.web;
using system.collections;
using system.web.services;
using system.web.services.protocols;
using newtonsoft.json;

class employee
{
  private string[] employeeinfo;
  
  public int employeeid;
  public string employeename;
  public string[] employeeinfo
  {
    get { return this.employeeinfo; }
    set { this.employeeinfo = value;}
  }
}

/**//// <summary>
/// webservice 的摘要说明
/// </summary>
[webservice(namespace = "http://tempuri.org/")]
[webservicebinding(conformsto = wsiprofiles.basicprofile1_1)]
public class webservice : system.web.services.webservice {

  public webservice () {

    //如果使用设计的组件,请取消注释以下行 
    //initializecomponent(); 
  }

  [webmethod]
  public string tojsonstring(int employeeid, string employeename, string[] employeeinfo) 
  {
    employee employee = new employee();
    employee.employeeid = employeeid;
    employee.employeename = employeename;
    employee.employeeinfo = employeeinfo;

    return javascriptconvert.serializeobject(employee);
  }

  [webmethod]
  public string tostremployee(string strjson)
  {
    employee decerializedemployee = (employee)javascriptconvert.deserializeobject(strjson, typeof(employee));
    return "id: " + decerializedemployee.employeeid + " "
      + "name: " + decerializedemployee.employeename + " "
      + "info: " + decerializedemployee.employeeinfo.tostring();
  }  
}

成员的属性类型分别为数字、字符串和数组。

5、对两个button编写事件代码

protected void btntojsonstring_click(object sender, eventargs e)
  {
    myserv.webservice mywebserv = new myserv.webservice();
    string employeejson = mywebserv.tojsonstring(int32.parse(txtid.text), txtname.text, txtinfo.text.split(','));
    txtjson.text = employeejson;
  }
  protected void btntostremployee_click(object sender, eventargs e)
  {
    myserv.webservice mywevserv = new myserv.webservice();
    string stremployee = mywevserv.tostremployee(txtjson.text);
    txtstremployee.text = stremployee;
  } 

6、按ctrl + f5运行;在employeeid、employeename、employeeinfo中输入123、hunts.c及一些个人信息(用逗号隔开);点击invoke tojsonstring,经服务器端序列化后,结果在txtjson文本框中;然后点击invoke tostremployee,此时txtjson文本框中的json文本传输给服务器端,服务器端读取该json并反序列化成对象,而后在txtstremployee中写入employee的成员值。

                 

在asp.net中如何使用json就介绍到这里,希望这篇文章对大家的学习有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网