AngularJS添加和推送嵌套的JSON对象

时间:2016-04-28 10:36:00

标签: javascript angularjs json push

我有一个像这样的JSON对象:

{
    "Name": "Shivansh",
    "RollNo": "1",
    "Stream": "CSE",
    "OverallScore": "76",
    "Semester": [
        {
            "SemesterName": "FY-2012 - 1",
            "TotalScore": "78.00",
            "StartDate" : "2012-02-14",
            "EndDate" : "2012-07-16",
            "Amount" : "55000",
            "Subject": [
                {
                    "subjectname": "maths",
                    "score": "81"
                },
                {
                    "subjectname": "chemistry",
                    "score": "79"
                },
                {
                    "subjectname": "physics",
                    "score": "77"
                }
            ]
        },
        {
            "SemesterName": "FY-2013-1",
            "TotalScore": "76.00",
            "StartDate" : "2013-02-16",
            "EndDate" : "2014-07-16",
            "Amount" : "55000",
            "Subject": [
                {
                    "subjectname": "ADA",
                    "score": "80"
                },
                {
                    "subjectname": "Operating System",
                    "score": "77"
                },
                {
                    "subjectname": "Databases",
                    "score": "73"
                },
                {
                    "subjectname": "Economics",
                    "score": "71"
                }
            ]
        }                
    ]
}

现在我想使用angularJS将另一个学期字段添加到此JSON中。任何人都可以帮助我如何实现这一目标。我要添加的下一个字段可能如下:

{
                    "SemesterName": "FY-2013-2",
                    "TotalScore": "75.00",
                    "StartDate" : "2011-02-16",
                    "EndDate" : "2012-07-16",
                    "Amount" : "55067800",
                    "Subject": [
                        {
                            "subjectname": "Sets and Probability",
                            "score": "78"
                        },
                        {
                            "subjectname": "Networking and Security",
                            "score": "76"
                        },
                        {
                            "subjectname": "Advanced DataBases",
                            "score": "72"
                        },
                        {
                            "subjectname": "Economics-2",
                            "score": "70"
                        }
                    ]
                }

到目前为止,我正在使用如下所述的这种类型的控制器:

$scope.addRowSubject = function() {
            $scope.insertsub = {
                //id = $scope.getSubject.length+1;
                subjectname : '1',
                score : '1'
            };

            $scope.getSubject.push($scope.insertsub);
};

getSubject是一个学期领域中的科目列表。我能够毫无问题地获得此列表。

$scope.addRowSemester = function() {
            $scope.insertsem = {
                //id = $scope.getSemester.length+1;
                StartDate : "1",
                EndDate : "1",
                Amount : "1",
                SemesterName : "1",
                TotalScore : "1",
                Subject : ""
            }
$scope.getSemester.push($scope.insertsem);
};

getSemester是学生中的学期列表。

我可以在我的JSON中推送一个学期字段,但由于主题字段为null,我无法推送主题字段。我希望你能得到这个想法。 所以关于这个的任何建议.. 提前致谢。

1 个答案:

答案 0 :(得分:1)

jsonObject的主题属性应该是数组的实例,如下所示:

$scope.addRowSemester = function() {
            $scope.insertsem = {
                //id = $scope.getSemester.length+1;
                StartDate : "1",
                EndDate : "1",
                Amount : "1",
                SemesterName : "1",
                TotalScore : "1",
                Subject : []
            }