2015-03-13 26 views
0

我有一个Web服务,它返回一个序列化的JSON数据字符串。返回的例子是:

{“Campus”:“BMSB”,“Program”:null,“Selected”:“false”,“Status”:“NEW”,“Status2”:null ,“Status3”:null,“StudentID”:00000,“StudentFirstName”:“Ioneu0027a”,“StudendMiddle Name”:“”,“StudentLastName”:“Byra”}, {“Campus”:“BMFW” “:”Accounting-Diploma“,”Selected“:”false“,”Status“:”GRAD“,”Status2“:”GRAD“,”Status3“:null,”StudentID“:00000,”StudentFirstName“ “,”StudendMiddleName“:”I“,”StudentLastName“:”Eib“}

两组相同的数据用逗号分隔。 现在我已将此代码硬编码到我的控制器中,并且加载正常。 我的代码设置范围变量。

soaFactory.getStudent(studentnumber,carslocation) 
    .success(function (response){ 

     if(JSON.parse(response) == "Object reference not set to an instance of an object.") 

     { 
      //this code will hide any previous student info if a student is not returned 
      $scope.noStudentReturned = false; 
      $scope.students = null; 
      $scope.curStudentFirstName= null; 
      $scope.curStudentLastName = null; 
      $scope.curStudentSchool= null; 
      $scope.curStudentStatus= null; 
      $scope.curStudentProgram= null; 
     } 
     else 
     { 
     var parsedResponse= JSON.parse(response); 
      //alert("parsedResponse :" + parsedResponse); 
     //var parsedAgain= JSON.parse(parsedResponse); 
     // alert("parseAgain: " + parsedResponse); 

     // $scope.students = [parsedResponse]; 
     $scope.students = [JSON.parse(parsedResponse)]; 
     $scope.noStudentReturned = true; 
       if ($scope.students.length == 1) 
       { 
         //if only one student returned call this function. 
       $scope.setStudent(e,0); 
       } 
       else 
       { 
       } 
     } 
    }) 
    .error(function (error){ 
      alert(error);    
      $scope.noStudentReturned = false; 
      $scope.students = null; 
      $scope.curStudentFirstName= null; 
      $scope.curStudentLastName = null; 
      $scope.curStudentSchool= null; 
      $scope.curStudentStatus= null; 
      $scope.curStudentProgram= null; 

    }); 

重点线路

   $scope.students = [JSON.parse(parsedResponse)]; 

调试器只是说语法错误。 现在这个代码对于一组数据工作正常。但不超过一个。

请帮 感谢 乔

回答

0

你是返回的JSON是无效的。当像你一样返回多个对象时,你需要将它们返回到一个数组中,而不是用逗号分隔的对象。它使用一组数据,因为您只发送一个有效的JSON对象。

因此,要解决这个问题,您需要将对象已经返回到数组中,这是更好的方法。或者您需要解析您的响应,以便可以在运行json.parse之前将每个对象解析为它自己的字符串,然后将其添加到数组中。底线是你想解析无效的JSON。

您的JSON无效,因为当您有两个对象未包含在数组中时,它具有多个根元素。一个对象是一个根元素(有效),两个对象是两个根元素(无效),一个数组中的两个对象等于一个根元素(有效)。数组就是根元素。通过类似http://jsonformatter.curiousconcept.com/的方式运行您的JSON,以便亲自查看问题。

0

你的JSON数据不会验证原因有二:

  1. 你需要用它们为对象的JSON字符串
  2. 价值观应该被引用的数组。

事实是,在某些情况下,你可能会摆脱它,但如果你正在处理数字,则不会。

因此,"StudentID":00000永远不会被解析,但"StudentID":"00000"会。底线:将你的对象作为一个数组包裹起来,就像nweg说的那样,把引号放在所有的值上,并且它会被解析得很好。

例如,这得到精细解析:

var test = '{"Campus":"BMSB","Program":null,"Selected":"false","Status":"NEW","Status2":null ,"Status3":null,"StudentID":"00000","StudentFirstName":"Ioneu0027a","StudendMiddle Name":"","StudentLastName":"Byra"}, {"Campus":"BMFW","Program":"Accounting- Diploma","Selected":"false","Status":"GRAD","Status2":"GRAD","Status3":null,"StudentID":"00000","StudentFirstName":"Kathryn","StudendMiddleName":"I","StudentLastName":"Eib"}'; 
    var testparse = JSON.parse('[' + test + ']'); 

我和你的JSON字符串唯一改变的事情是在几个数值的报价。

+0

这是不正确的。 JSON允许使用整数,浮点数,空值,字符串(双引号)和布尔值(http://json.org/) – goreorto 2015-03-14 00:13:19

+0

将null作为无引号的值进行尝试,它将很好地解析。至少由一些口译员使用字符串值也是如此,这就是为什么我说你可能会逃避它。我们不是说一些不同的东西,除非你提到的布尔值不一定用双引号。 – alou 2015-03-14 17:10:34

1

我有一个Web服务,返回一个序列化的JSON数据字符串。返回的示例是:

{“Campus”:“xxxx”,...,“StudentLastName”:“xxxx”},{“Campus”:“xxxx”,...,“ StudentLastName “:” XXXX“}

这不是一个字符串,这些都是用逗号分开的两个JSON对象。或者你忘了粘贴外部引号。

如果您的服务可能会返回多个学生,这看起来是通过查看您的代码的情况,确保您收到作为响应包装在数组中的对象。这样的:由服务返回

学生:

[{ 
    "Campus": "4444", 
    "Program": null, 
    "Selected": "false", 
    "Status": "NEW", 
    "Status2": null, 
    "Status3": null, 
    "StudentID": 00000, 
    "StudentFirstName": "somename", 
    "StudendMiddle Name": "", 
    "StudentLastName": "somename" 
}, 
{ 
    "Campus": "ssdd", 
    "Program": "smith", 
    "Selected": "false", 
    "Status": "xxxx", 
    "Status2": "xxxx", 
    "Status3": null, 
    "StudentID": 00000, 
    "StudentFirstName": "somename", 
    "StudendMiddleName": "I", 
    "StudentLastName": "somename" 
}]; 

然后在你的成功处理程序你只需要读这个数组。哦,对了,如果你要检查,如果学生被退回或不,请不要永远不会写类似的东西:

if (JSON.parse(response) == "Object reference not set to an instance of an object.") { 
    // if a student is not returned 
} 

现在,学生们作为一个数组返回,测试可以简单地rewrited为:

if (response.length === 0) { 
    // no students 
} 
+0

谢谢。在我试图将json加载到范围之前,我并不认为我需要[]。还要感谢关于不使用对象的说明......我只是把它扔进测试中,并计划将它移出。再次感谢。 – 2015-03-14 01:22:42

相关问题