【问题标题】:AngularJS setting JSON string with more then an one set of data to a $scope variableAngularJS 将 JSON 字符串与一组以上的数据设置为 $scope 变量
【发布时间】:2015-03-13 18:52:07
【问题描述】:

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

{"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"}

两组相同的数据,用逗号分隔。 现在我在我的控制器中硬编码了这段代码,它加载得很好。 我设置范围变量的代码。

 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)];

调试器只是说语法错误。 现在这段代码适用于一组数据。但不超过一个。

请帮忙 谢谢 乔

【问题讨论】:

    标签: json angularjs


    【解决方案1】:

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

    {"校园":"xxxx",...,"StudentLastName":"xxxx"}, {"校园":"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
    }
    

    现在学生以数组的形式返回,测试可以简单地重写为:

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

    【讨论】:

    • 谢谢。在尝试将 json 加载到范围之前,我认为我不需要 []。还要感谢关于不使用对象的说明……我只是将其投入测试并计划将其移出。再次感谢。
    【解决方案2】:

    您的 json 数据无法验证有两个原因:

    1. 您需要将它们包装为对象数组
    2. JSON 字符串中的值应加引号。

    事实上,在某些情况下您可能会侥幸逃脱,但如果您处理的是数字,则不会。

    所以,"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 字符串中唯一改变的是几个数值中的引号。

    【讨论】:

    • 这不是真的。 JSON 允许整数、浮点数、空值、字符串(双引号)和布尔值(json.org
    • 尝试 null 作为不带引号的值,它将被很好地解析。至少某些解释器对字符串值也是如此,这就是为什么我说你可能会侥幸逃脱。我们并没有说什么不同,只是你提到的布尔值不必用双引号括起来。
    【解决方案3】:

    您返回的 JSON 无效。当像您一样返回多个对象时,您需要以数组的形式返回它们,而不是用逗号分隔的对象。它适用于一组数据,因为您只发送一个有效的 JSON 对象。

    因此,要解决此问题,您需要将对象发送回数组中,这是更可取的方法。或者您需要以一种可以将每个对象解析为它自己的字符串的方式解析您的响应,然后再对其运行 json.parse 然后将其添加到数组中。底线是您正在尝试解析无效的 JSON。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 2019-09-29
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多