简体   繁体   English

asp.net Web服务和json中的额外查询!

[英]asp.net web service and extra qouatation in json result !

I want to call a web service method in javascript. 我想用javascript调用网络服务方法。 (asp.net 3.5) (asp.net 3.5)

I have traced the result with firebug . 我已经用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: //编辑:在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 : 并在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; 

thank you . 谢谢 。

mir MIR

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. b [“ d”]将返回一个字符串,而不是对象数组。 You can either go around this with the following in javascript: 您可以使用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. 或者更好的方法,发布返回此代码的代码,我们可以尝试找出为什么将其作为字符串返回。

You shouldn't need to serialize manually in the web service; 您不需要在Web服务中手动进行序列化; 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). 由于您使用string作为返回对象,因此在序列化时(再次)它将为您转换为string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM