web 服务JSON数据

WEB Service返回JSON

(1)    JSON

是存储和交换文本信息的语法。类似 XML。

例如:

{
"szjm":{
"太湖清水蟹":"注册商标:太湖清水蟹 规格:公蟹4.3-4.5,母蟹3.3-3.5 价格:108元/对 生产厂商:苏州市太湖蟹养殖场 等级:1级 质量认证:绿色食品 二维码:15121203",
"溯源信息":"2012/11/18 12:32 水温: 26.04℃ 溶解氧:8.15毫克/升 PH值:7.67 氢:0.08毫克/升 有机物耗氧:4.72毫克/升",
"查询次数":"24",
"核销":"3CE171964E6F313AAC27ED7AF4F0C3B6"}
}

•JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)

•JSON 是轻量级的文本数据交换格式

•JSON 独立于语言

•JSON 具有自我描述性,更易理解

(1)    调用WEB服务返回JSON,同时实现

http://IP/JSONService.asmx/TestychJSON?erwID=15121203

调用,实现不同语言的调用。

传递参数erwID为15121203。

A . Web.config如下:

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
      <webServices>
        <protocols>
          <add name="HttpSoap"/>
          <add name="HttpPost"/>
          <add name="HttpGet"/>
          <add name="Documentation"/>
        </protocols>
      </webServices>
      <httpHandlers>
        <remove verb="*" path="*.asmx"></remove>
        <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions,      Version=1.0.61025.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35">
        </add>
      </httpHandlers>
    </system.web>
</configuration>

主要说明:

 

<!--控制 ASP.NET Web 服务及其客户端的行为。protocols:指定传输协议,ASP.NET 可使用这些传输协议来解密 HTTP-->

B. 一个简单的返回JSON 的代码

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void HelloWorld() 
{ 
 Context.Response.Write("{ \"state\": { \"return\": \"true\", \"info\": \"\", \"code\": \"\" } }"); 
 Context.Response.End();
}

C 读取数据信息,返回JSON数据

   [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void  TestychJSON(string erwID)
        {
            if (erwID.Trim() == "")
            {
                return ;
            }
            ych_Web[] e = new ych_Web[1];
            e[0] = new ych_Web();
            //e[0].二维码 = erwID;
            //根据erwID查找记录
            //str_SQL
            /*
               ,[BrandName]
      ,[Type]
      ,[Degree]
      ,[QualityCertification]
      ,[CertificationBody]
      ,[Address3]
      ,[TracingTime]             
             */
            string str_SQL = "SELECT  [ID] ,[erwID]  ,[name] ,[Address] ,[erwPhoto]      ,[Manufacturers]      ,[Address2]      ,[ContactphoneNum]      ,[Specifications]      ,[Price]      ,[WateTemperature]      ,[DissolveOxygn]      ,[PHNum]      ,[Hydrogen]      ,[Azote]      ,[COD]      ,[BiologicalIndicators]    ,[BrandName]       ,[Type]      ,[Degree]      ,[QualityCertification]      ,[CertificationBody]      ,[Address3]      ,[TracingTime],[SearchNum] ,[Verification]     FROM  [ych_Web] ";
            //str_SQL = str_SQL+ " where erwID='" + erwID + "'";
            str_SQL = str_SQL + " where erwID='" + erwID.Replace("'", "\"") + "'";
            DataTable dt = get_dt(str_SQL);
            
            if (dt.Rows.Count > 0)
            {
                //ych_Web_target[] e1 = new ych_Web_target[1];

                //e[0].ID = int.Parse(dt.Rows[0]["ID"].ToString());
                //基本信息
               // e[0].基本信息 = "二维码:" + dt.Rows[0]["erwID"].ToString();
                e[0].基本信息 = "注册商标:"+dt.Rows[0]["name"].ToString();
                e[0].基本信息 += "  规格:" + dt.Rows[0]["Specifications"].ToString();
                e[0].基本信息 += "  价格:"+dt.Rows[0]["Price"].ToString();                
                e[0].基本信息 += "  生产厂商:" + dt.Rows[0]["Manufacturers"].ToString();               
                e[0].基本信息 +=" 等级:" +dt.Rows[0]["Degree"].ToString()+"  质量认证:"+dt.Rows[0]["QualityCertification"].ToString();
                e[0].基本信息 += " 二维码:" + dt.Rows[0]["erwID"].ToString();
                //e[0].二维码 = dt.Rows[0]["erwID"].ToString();
                //e[0].名称 = dt.Rows[0]["name"].ToString();
                //e[0].价格 = dt.Rows[0]["Price"].ToString();
                //e[0].规格 = dt.Rows[0]["Specifications"].ToString();

                //e[0].Address = dt.Rows[0]["Address"].ToString();
                //e[0].erwPhoto = dt.Rows[0]["erwPhoto"].ToString();
                //e[0].生产厂商 = dt.Rows[0]["Manufacturers"].ToString();
                //e[0].Address2 = dt.Rows[0]["Address2"].ToString();
                //e[0].ContactphoneNum = dt.Rows[0]["ContactphoneNum"].ToString();
                //e[0].Specifications = dt.Rows[0]["Specifications"].ToString();
                //e[0].Price = dt.Rows[0]["Price"].ToString();
               // e[0].等级 = dt.Rows[0]["Degree"].ToString()+"  质量认证:"+dt.Rows[0]["QualityCertification"].ToString();                
                //e[0].认证单位 = dt.Rows[0]["CertificationBody"].ToString();
                e[0].溯源信息 =  dt.Rows[0]["TracingTime"].ToString();
                e[0].溯源信息 += "  水温: " + dt.Rows[0]["WateTemperature"].ToString();
                e[0].溯源信息 += "  溶解氧:" + dt.Rows[0]["DissolveOxygn"].ToString();
                e[0].溯源信息 += "  PH值:" + dt.Rows[0]["PHNum"].ToString();
                e[0].溯源信息 += "  氢:" + dt.Rows[0]["Hydrogen"].ToString();
                e[0].溯源信息 += "  有机物耗氧:" + dt.Rows[0]["COD"].ToString(); 
                 //e[0].理化指标.水温 = dt.Rows[0]["WateTemperature"].ToString();
                 //e[0].理化指标.溶解氧 = dt.Rows[0]["DissolveOxygn"].ToString();
                 //e[0].理化指标.PH值 = dt.Rows[0]["PHNum"].ToString();
                 //e[0].理化指标.氢 = dt.Rows[0]["Hydrogen"].ToString();
                 //e[0].理化指标.氮 = dt.Rows[0]["Azote"].ToString();
                 //e[0].理化指标.有机物耗氧 = dt.Rows[0]["COD"].ToString();
               
                //e[0].BrandName = dt.Rows[0]["BrandName"].ToString();
                //e[0].Type = dt.Rows[0]["Type"].ToString();
                //e[0].Address3 = dt.Rows[0]["Address3"].ToString();               
                //e[0].生物指标 = dt.Rows[0]["BiologicalIndicators"].ToString();                
                int i_current = 0;
                if ((dt.Rows[0]["SearchNum"] == null) || (dt.Rows[0]["SearchNum"].ToString().Trim() == ""))
                {
                    str_SQL = "update ych_Web set SearchNum='1' where ID= " + dt.Rows[0]["ID"].ToString();
                    i_current = 1;

                }
                else
                {                   
                    i_current = int.Parse(dt.Rows[0]["SearchNum"].ToString());
                    i_current++;
                    str_SQL = "update ych_Web set SearchNum='" + i_current + "' where ID= " + dt.Rows[0]["ID"].ToString();
                }
                exec_SQL(str_SQL);
                e[0].查询次数 = i_current.ToString();
                e[0].核销 = MD5_Encrypt(dt.Rows[0]["erwID"].ToString());
                string strreturn = new JavaScriptSerializer().Serialize(e);
                strreturn=strreturn.Replace("[", "");
                strreturn=strreturn.Replace("]", "");
                strreturn = strreturn.Replace("基本信息", dt.Rows[0]["name"].ToString());
                //strreturn = strreturn.Remove(strreturn.Length - 1, 1);
                //strreturn = strreturn.Remove(0, 1);
                
                //response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(strreturn));  
                //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//设置输出流为简体中文    
                //Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF8");//设置输出流为简体中文
                //HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.UTF8;
                strreturn = "{\"szjm\":" + strreturn + "" + "}";
                Context.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
                //return strreturn;
                Context.Response.Write(strreturn);
                Context.Response.End();

            }
            else
            {
                //response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(""));  
                return ;
            }
        }

注意:

这里代码中

Context.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());

Context.Response.Write(strreturn);

Context.Response.End();

实现将输出写入 HTTP 输出流,

 

posted @ 2013-07-06 09:38  jhtchina  阅读(411)  评论(0)    收藏  举报