相关疑难解决方法(0)

如何在JSON对象上序列化__type属性

我从a WebMethod中返回的每个对象都ScriptService被包装到一个JSON对象中,并且数据位于一个名为的属性中d.没关系.但我不希望将附加__type属性提供给客户端,因为我使用jQuery进行手动处理.

可能吗?

c# asp.net json asmx javascriptserializer

62
推荐指数
5
解决办法
3万
查看次数

如何从webservice返回JSON

早上,

我需要从我的网络服务返回一条消息.下面是我的代码示例,我将返回一个字符串.

[web method]
public string CheckFeedSubmission()
    {
        string responseText = "";
        try
        {
            //Stuff goes here
            responseText = "It Worked!"
        }
        catch (Exception ex) { responseText = "Opps wehave an error! Exception message:" + ex.Message; }
        return responseText ;
    }
Run Code Online (Sandbox Code Playgroud)

我目前得到以下回复......

<string xmlns="http://tempuri.org/"/>
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望返回类似的内容

 {"success" : true, "message" : "***Message Here***"}
Run Code Online (Sandbox Code Playgroud)

我相信一旦我理解了它,我将能够在需要时返回其他物品.它只是这个基础我需要解决.

非常感谢所有帮助,在此先感谢:)

更新:刚发现这个......

 return "{Message:'hello world'}"
Run Code Online (Sandbox Code Playgroud)

我需要类似的东西吗?

 responseText = "{"success" : true, "message" : \"There has been an error. Message: " + ex.Message + "\"}"
Run Code Online (Sandbox Code Playgroud)

c# asp.net json web-services

10
推荐指数
1
解决办法
5万
查看次数

创建一个返回JSON而不是XML的ASP.net WebService

也许该方法正在返回它应该如何,但我基本上只是做了一个看起来像这样的测试方法

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string TestJSON()
    {
        var location = new Location[2];
        location[0] = new Location();
        location[0].Latitute = "19";
        location[0].Longitude = "27";
        location[1] = new Location();
        location[1].Latitute = "-81.9";
        location[1].Longitude = "28";

        return new JavaScriptSerializer().Serialize(location);
    }
Run Code Online (Sandbox Code Playgroud)

当我从我的Android应用程序中点击这个时,我得到了这样的异常

Value <?xml of type java.lang.String cannot be converted to JSONArray
Run Code Online (Sandbox Code Playgroud)

我认为这个方法只返回JSON,但这就是Web服务方法返回的内容

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">[{"Latitute":"19","Longitude":"27"},{"Latitute":"-81.9","Longitude":"28"}]</string>
Run Code Online (Sandbox Code Playgroud)

是假设是这样的吗?有没有办法删除JSON之外的XML东西?我不确定我必须在我的web服务中做什么才能使它返回正确的数据格式

在Android上使用代码调用Web服务

   public String readWebService(String method)
{
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("http://myserver.com/WebService.asmx/" + method);


    Log.d(main.class.toString(), …
Run Code Online (Sandbox Code Playgroud)

xml asp.net android json web-services

6
推荐指数
1
解决办法
6385
查看次数

如何从vb.net中的aspx.vb页面调用webmethod

我想做的是从aspx.vb调用WebMethod,下面是Default.aspx.vb中的WebMethod语法

<System.Web.Services.WebMethod()> _
<ScriptMethod(UseHttpGet:=True, ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function dat( _
ByVal Id As Integer) As List(Of items)
    Dim eve As New List(Of items)()
    eve = (From row In getItems(Id).Rows
           Select New items With {
                                .Name = row("Name").ToString(),
                                .Description = row("Description").ToString(),
                                .ItemPic_url = row("ItemPic_url").ToString()}).ToList()
    Return eve
End Function
Run Code Online (Sandbox Code Playgroud)

以下是我从中调用Web方法的jquery函数:

注意:我的Jquery函数位于我的母版页中,我从启动Default.aspx页调用它。

function getItems() {
        $("#tbody").empty();
        var id = $("select")[0].value;
        $.ajax({
            url: "Default.aspx/dat",
            data: { Id: id },
            contentType: "Application/json; charset=utf-8",
            responseType: "json",
            method: "POST",
            success: function (response) {
                $("#tbody").empty();
                var rows …
Run Code Online (Sandbox Code Playgroud)

javascript vb.net asp.net jquery

6
推荐指数
1
解决办法
1万
查看次数