C# 读取从ASMX返回的JSON数据
我已经写了一个ASMX服务,看起来像这样C# 读取从ASMX返回的JSON数据,c#,jquery,json,asmx,C#,Jquery,Json,Asmx,我已经写了一个ASMX服务,看起来像这样 namespace AtomicService { [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] [ScriptService] public class V
namespace AtomicService
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class Validation : WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string IsEmailValid(string email)
{
Dictionary<string, string> response = new Dictionary<string, string>();
response.Add("Response", AtomicCore.Validation.CheckEmail(email).ToString());
return JsonConvert.SerializeObject(response, Formatting.Indented);
}
}
}
虽然我可以从msg.d
中看到数据,但我无法访问它。我想知道响应是什么。我怎样才能做到呢
我不完全相信我的ASMX在所有工作中都返回了正确类型的JSON
有人能帮忙吗?:) 反序列化的JSON对象似乎有另一个JSON作为其值之一。尝试添加:
var data = $.parseJSON(msg.d);
alert(data.Response);
为你的成功回拨电话,看看情况是否如此
更新:如果是这种情况,那么您将对数据进行两次JSON编码–有关正确的解决方案,请参阅。您的反序列化JSON对象似乎将另一个JSON作为其值之一。尝试添加:
var data = $.parseJSON(msg.d);
alert(data.Response);
为你的成功回拨电话,看看情况是否如此
更新:如果是这种情况,那么您需要对数据进行两次JSON编码–请参阅以获得正确的解决方案。@rsp的回答在技术上是正确的,但真正的问题是您在asmx页面中对值进行了双重编码
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] //This will cause the response to be in JSON
public Dictionary<string, string> IsEmailValid(string email)
{
Dictionary<string, string> response = new Dictionary<string, string>();
response.Add("Response", AtomicCore.Validation.CheckEmail(email).ToString());
return response; //Trust ASP.NET to do the formatting here
}
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]//这将导致响应采用Json格式
公共字典IsEmailValid(字符串电子邮件)
{
字典响应=新建字典();
Add(“response”,AtomicCore.Validation.CheckEmail(email.ToString());
return response;//请信任ASP.NET在此处进行格式化
}
然后,您不需要在JavaScript中进行双重解码。@rsp的答案在技术上是正确的,但真正的问题是您在asmx页面中对值进行了双重编码
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] //This will cause the response to be in JSON
public Dictionary<string, string> IsEmailValid(string email)
{
Dictionary<string, string> response = new Dictionary<string, string>();
response.Add("Response", AtomicCore.Validation.CheckEmail(email).ToString());
return response; //Trust ASP.NET to do the formatting here
}
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]//这将导致响应采用Json格式
公共字典IsEmailValid(字符串电子邮件)
{
字典响应=新建字典();
Add(“response”,AtomicCore.Validation.CheckEmail(email.ToString());
return response;//请信任ASP.NET在此处进行格式化
}
那么你就不需要在JavaScript中进行双重解码了。@C.Ross:你解决问题的速度比原来的海报快,你可以验证双重JSON编码是否确实是这里的问题。我会更新我的答案以参考你的答案。这非常适合rsp!非常感谢。然而,我很好奇C.罗斯的回答,但你已经解决了我原来的问题。非常感谢:)@C.Ross:你解决问题的速度比原来的海报快,因为你验证了双重JSON编码是否确实是这里的问题。我会更新我的答案以参考你的答案。这非常适合rsp!非常感谢。然而,我很好奇C.罗斯的回答,但你已经解决了我原来的问题。非常感谢:)谢谢你的回复。我已经按照您的建议做了(在返回响应;
上需要一个.ToString()
),但是响应是作为字符串的对象类型,而不是对象本身。我在以前版本的服务中使用了这个,无意中发现一个博客建议我序列化?@dooburt表示它确实为您进行了序列化。我不知道这是不是字典的问题…@Doobert我想我已经弄明白了。因为返回类型是string,所以必须将其转换为string。返回我的编辑显示的实际对象类型,然后ASP.NET将为您将其格式化为JSON。感谢您回来-非常感谢:)我将尝试您对我正在工作的项目提出的建议,并回来更新您+我只是为了回来;)谢谢,这让我很长时间都发疯了!我想现在不需要JsonConvert.SerializeObject.:)谢谢你的回复C.罗斯。我已经按照您的建议做了(在返回响应;
上需要一个.ToString()
),但是响应是作为字符串的对象类型,而不是对象本身。我在以前版本的服务中使用了这个,无意中发现一个博客建议我序列化?@dooburt表示它确实为您进行了序列化。我不知道这是不是字典的问题…@Doobert我想我已经弄明白了。因为返回类型是string,所以必须将其转换为string。返回我的编辑显示的实际对象类型,然后ASP.NET将为您将其格式化为JSON。感谢您回来-非常感谢:)我将尝试您对我正在工作的项目提出的建议,并回来更新您+我只是为了回来;)谢谢,这让我很长时间都发疯了!我想现在不需要JsonConvert.SerializeObject.:)