C# WebService return json

本文关键字:json return WebService | 更新日期: 2023-09-27 18:09:44

我需要从下面的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> 

C# WebService return json

下面的语句返回一个json响应

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

使用Newtonsoft。Jsonhttp://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;
            }

您必须使用Newtonsoft.Json;

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

正确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":"No fromFormat received .","success":false}