0

我有一个来自 wcf servicein jquery 的 json 数据,像这样

GetBedTypeList1Result1 是 wcf 中的函数

{
    "GetBedTypeList1Result":[
        {"Code":23,"CompanyCode":null,"Decode":"1 Class New Born Bed","DivisionCode":0,"LocationCode":0,"admDueDepAmt":0,"bedTypeCode":0,"caseTypeCode":0,"caseTypeDecode":null,"ptnClassCode":0,"ptnClassDecode":null,"rsvDueDepAmt":0},
        {"Code":22,"CompanyCode":null,"Decode":"1st Class Bed","DivisionCode":0,"LocationCode":0,"admDueDepAmt":0,"bedTypeCode":0,"caseTypeCode":0,"caseTypeDecode":null,"ptnClassCode":0,"ptnClassDecode":null,"rsvDueDepAmt":0},
        {"Code":5,"CompanyCode":null,"Decode":"Classique Bed","DivisionCode":0,"LocationCode":0,"admDueDepAmt":0,"bedTypeCode":0,"caseTypeCode":0,"caseTypeDecode":null,"ptnClassCode":0,"ptnClassDecode":null,"rsvDueDepAmt":0}
    ],
    "strErrMsg":"Y",
    "chrErrFlg":"c"
}

我正在调用如下服务

         function CallWcfService() {
         //alert("CallWcfServicexxxx");
         jQuery.ajax
        (
         {

             type: Type,
             url: Url,
             data: parameters,
             contentType: ContentType, // content type sent to server
             dataType: DataType, //Expected data format from server
             cache: "false",
             crossDomain: true,   //Same result if i remove this line
             processdata: ProcessData, //True or False
             success: function (msg) 
             {
                 ServiceSucceeded(msg);
             },
             error: ServiceFailed// When Service call fails
         }
       );
     }



     function callService() 
     {
         DataType = "json";
         Type = "GET";
         var par = 4;
         parameters = null;
         Url = "http://192.168.2.42/CWSERVERWCF/bedtypemasterService.svc/GetBedTypeList?callback=?";
         parameters = "{'strErrMsg':'1'},{'chrErrFlg':'A'},{'pcompanycode':'0'},{'pdiv':'1'},{'ploc':'1'}";
        // alert(parameters);
         ContentType = "application/json; charset=utf-8";
         ProcessData = true;
         //alert("sssssasasadsds");
         CallWcfService();
     }

我正在尝试获取数据但没有得到下面的 lke

   function ServiceSucceeded(result)
      {

        if (DataType == "json")
        {
           var obj =  jQuery.parseJSON(JSON.stringify(JSON.stringify(result)));
           for (var x = 0; x < obj.length; x++) 
            {

            }
         }
     }

在 obj.length 中,字符数即将到来,并且 jQuery.parseJSON(result) 不起作用

请帮忙

4

2 回答 2

0

尝试使用JSON.parse(result)而不是:var obj = jQuery.parseJSON(JSON.stringify(JSON.stringify(result)));

此外,由于您dataType在 $.ajax 调用中提到了 as 'json',因此您的响应应该已经是 JSON 格式,不需要解析。

于 2013-07-11T12:32:42.967 回答
0

如果结果是 json 则不需要这样解析。如果数据类型设置为 json,jQuery.ajax 将返回一个 javascript 对象。

因此,在您的 ServiceSucceeded 函数中,您可以直接对结果变量进行操作。如果您尝试遍历床类型,请将 for 循环更改为以下内容:

for (var i = 0; i < result.GetBedTypeList1Result.length; i++) {
  // ...do stuff
  // var bed = result.GetBedTypeList1Result[i]]
}
于 2013-07-11T12:34:25.157 回答