C#Web服务返回json

时间:2014-06-20 10:07:48

标签: c# json post

我需要从此Web服务返回一条消息,但它必须是json格式。现在可以实现吗?

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

namespace WebApplication1
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public response HelloWorld()
        {
            response obj = new response
            {
                message = "No fromFormat recieved.",
                success = false
            };

            return obj;
        }
    }

    public class response
    {
        public string message = "";
        public Boolean success = false;
    }
}

jQuery代码:

//contentType: "application/json" if I add this line, response is undefined.
$('document').ready(function () {
    $.ajax({
        url: 'http://exampleUrl.com/WebService1.asmx/HelloWorld',
        type: 'POST',
        success: function (data) {
            console.log(data.responseText);
        },
        error: function (data) {
            console.log(data.responseText);
        },
        async: false,
        dataType: "json",
    });
});

响应:

<?xml version="1.0" encoding="utf-8"?>
<response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <message>No fromFormat recieved.</message>
  <success>false</success>
</response> 

4 个答案:

答案 0 :(得分:1)

以下语句返回json响应

string json = JavaScriptSerializer.Serialize({"results":[{"id":1,"value":"ABC","info":"ABC"},{"id":2,"value":"XYZ","info":"XYZ"});
return json;

答案 1 :(得分:1)

使用Newtonsoft.Json http://james.newtonking.com/json/help/index.html?topic=html/M_Newtonsoft_Json_JsonConvert_SerializeObject.htm

  public response HelloWorld()
            {
                response obj = new response
                {
                    message = "No fromFormat recieved.",
                    success = false
                };

                string Result = JsonConvert.SerializeObject(obj);
                return obj;
            }

答案 2 :(得分:0)

您必须使用Newtonsoft.Json;

public string HelloWorld()
        {
            response obj = new response
            {
                message = "No fromFormat recieved.",
                success = false
            };
            var jsondata = JsonConvert.SerializeObject(obj);
          return jsondata.ToString();
        }

答案 3 :(得分:0)

更正C#代码:

public String HelloWorld()
{
    response obj = new response
    {
        message = "No fromFormat recieved.",
        success = false
    };

    return new JavaScriptSerializer().Serialize(obj);
}

正确的jQuery代码是:

$('document').ready(function () {
    $.ajax({
        url: 'http://exampleUrl.com/WebService1.asmx/HelloWorld',
        type: 'POST',
        success: function (data) {
            console.log(data);
        },
        error: function (data) {
            console.log(data);
        },
        async: false,
        dataType: "json",
    });
});

响应:

  

{“message”:“没有收到来自格式。”,“成功”:false}