JSON格式输出-Angular

时间:2017-05-12 08:48:36

标签: json angular

我意识到json输出的格式可能会导致应用程序出错。我怎样才能在下面的代码中获得这种搜索格式?我无法真正看到我的错误来自哪里。虽然,我能够看到我的数据。我希望对象在主题中,但是在我的响应中,对象位于主题中的另一个数组(0:[])中。如何在主题下获取对象。

JSON格式 - 首选

lesson: "Angular 2 JSON"
topics:[]

    routing,
    navigation

我的JSON回复

 lesson: "Angular 2 JSON"
        topics:[]
         0:[]

            routing,
            navigation
来自后端的

响应

"tutorial": "Angular 2 Lessons",
  "topics": [
    {
      "id": 3,
      "name": "routing",

    },
    {
      "id": 3,
      "name": "params",

    }
  ],
  "number of  lessons": 2
}

//how i submit in backend test 

    "tutorials" : "Angular 2 lessons",
    "topics" : {
        "1" : {
             "id": 3,
          "name": "routing",

复选框html

<input  #{{Topics[key].id}} type="checkbox" (change)="checkbox(Topics[key])">

TS

 checkbox(Topics){
            Topics.selected = (Topics.selected) ? false : true;
            this.topicSelected = Topics;

        }

//and this is how i submit to the  array

//array 

 tutorials =   { 
                lessons : "",

                topics : [{
                names: "",


         }]

            }


submitJSON(){
   //topicSelected are a list of the topics selected.

   this.tutorials.topics['0'] = this.topicSelected;

}

0 个答案:

没有答案