asp.net Web 服务和 json 结果中的额外 qouatation !

发布于 2024-09-12 01:07:50 字数 1194 浏览 2 评论 0原文

我想在 javascript 中调用 Web 服务方法。 (asp.net 3.5)

我已经用 firebug 跟踪了结果。 这是结果:

{"d":"[{\"TI\":\"www\"},{\"TI\":\"www1\"}]"}

我认为正确的结果应该是这样的

{"d":[{\"TI\":\"www\"},{\"TI\":\"www1\"}]}

Bracket 之前和之后的 quotation 是什么?

// 编辑过的: 在 webserivce:

public class Test
    {
        public Test(string t){T1 = t;}
        public string T1 { set; get; }
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true, XmlSerializeString = false)]
    public string Load(string e)
    {
        List<Test> post = new List<Test> { new Test("www"), new Test("www1") };
        return JsonConvert.SerializeObject(post);
    }

和 js 文件中:

 var store = new Ext.data.JsonStore({
        proxy: new Ext.data.HttpProxy({
            url: '/core/webservice/service.asmx/Load',
            method: 'GET',
            headers: { 'Content-type': 'application/json' }
        }),
        root: 'd',
        id: 'Id',
        fields: ['TI']
    });
    store.load({ params: { e: ''} });
    return; 

谢谢。

米尔

I want to call a web service method in javascript. (asp.net 3.5)

I have traced the result with firebug .
here is the result:

{"d":"[{\"TI\":\"www\"},{\"TI\":\"www1\"}]"}

I think the correct result should like this

{"d":[{\"TI\":\"www\"},{\"TI\":\"www1\"}]}

what is the quotation before and after the Bracket ?

// edited :
in webserivce:

public class Test
    {
        public Test(string t){T1 = t;}
        public string T1 { set; get; }
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true, XmlSerializeString = false)]
    public string Load(string e)
    {
        List<Test> post = new List<Test> { new Test("www"), new Test("www1") };
        return JsonConvert.SerializeObject(post);
    }

and in js file :

 var store = new Ext.data.JsonStore({
        proxy: new Ext.data.HttpProxy({
            url: '/core/webservice/service.asmx/Load',
            method: 'GET',
            headers: { 'Content-type': 'application/json' }
        }),
        root: 'd',
        id: 'Id',
        fields: ['TI']
    });
    store.load({ params: { e: ''} });
    return; 

thank you .

mir

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

污味仙女 2024-09-19 01:07:50

您不需要在 Web 服务中手动序列化;考虑使用类似这样的东西:

public List<Test> Load(string e)
{
    List<Test> post = new List<Test> { new Test("www"), new Test("www1") };
    return post;
}

由于您使用 string 作为返回对象,它会在序列化它时将其转换为您的返回对象(再次)。

You shouldn't need to serialize manually in the web service; consider using something like this instead:

public List<Test> Load(string e)
{
    List<Test> post = new List<Test> { new Test("www"), new Test("www1") };
    return post;
}

Since you're using string as your return object, it will convert it as that for you when serializing it (once again).

我的影子我的梦 2024-09-19 01:07:50

引号表明这是一个字符串,因此 :

var b = {"d":"[{\"TI\":\"www\"},{\"TI\":\"www1\"}]"};

b["d"] 将返回一个字符串而不是对象数组。
您可以使用 javascript: 中的以下代码来解决此问题:

var c = eval(b["d"]);

这会将字符串转换为对象数组。
或者更好的方法是,发布返回它的代码,我们可以尝试找出为什么它作为字符串返回。

The quotation indicates that this is a string, so :

var b = {"d":"[{\"TI\":\"www\"},{\"TI\":\"www1\"}]"};

b["d"] will return a string instead of array of objects.
You can either go around this with the following in javascript:

var c = eval(b["d"]);

which will turn the string into an array of objects.
Or the better way, post the code that is returning this and we can try to figure out why it is returned as a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文