【问题标题】:Iterating through a JSON string in Angularjs在 Angularjs 中遍历 JSON 字符串
【发布时间】:2015-01-23 10:05:10
【问题描述】:

我正在托管一个公开 REST API 的 Web 服务器。以下是我从服务器获得的 JSON 响应。

[
{
    "score": 4,
    "sense": "be the winner in a contest or competition; be victorious; \"He won the Gold Medal in skating\"; \"Our home team won\"; \"Win the game\""
},
{
    "score": 2,
    "sense": "win something through one's efforts; \"I acquired a passing knowledge of Chinese\"; \"Gain an understanding of international finance\""
},
{
    "score": 0,
    "sense": "obtain advantages, such as points, etc.; \"The home team was gaining ground\"; \"After defeating the Knicks, the Blazers pulled ahead of the Lakers in the battle for the number-one playoff berth in the Western Conference\""
},
{
    "score": 4,
    "sense": "attain success or reach a desired goal; \"The enterprise succeeded\"; \"We succeeded in getting tickets to the show\"; \"she struggled to overcome her handicap and won\""
    }
]

我想在列表中显示这个。我正在以下列方式使用材料设计:

<md-list data-ng-repeat="item in sensesscores track by $index">
    <md-item-content>
    <div class="md-tile-content">
        {{item.sense}}
    </div>
    <div class="md-tile-left">
        {{item.score}}
    </div>
    </md-item-content>

</md-list>

在我的控制器中,我有以下内容:

$http.get('http://localhost:8080/nlp-wsd-demo/wsd/disambiguate').
        success(function(data) {
            $scope.sensesscores = data;
            console.log(data);
        });

我确保我能够获取“sensesscores”中的数据并将其打印在屏幕上。但是,我无法进行解析并将其显示在列表中。提前致谢。

编辑


我更改了代码以更正语法并将 ng-repeat 向上移动层次结构,但它仍然不起作用。但是,我在另一个 JSON 文件上进行了尝试,该文件有效。

[{
"sense": "sensaS,NF,ASNGD.,AD., BVAS.,GMDN,FG e1",
"score" : 5
},
{
"sense": "sen ASG SFG S H D GD FJDF JDF J GFJ FDFGse2",
"score" : 13
}
,
{
"sense": "sen ASG SFG S H D GD FJDF JDF J GFJ FDFGse2",
"score" : 1
},
{
"sense": "sen ASG SFG S H D GD FJDF JDF J GFJ FDFGse2",
"score" : 0
},
{
"sense": "sen ASG SFG S H D GD FJDF JDF J GFJ FDFGse2",
"score" : 3
},
{
"sense": "sen ASG SFG S H D GD FJDF JDF J GFJ FDFGse2",
"score" : 2
},
{
"sense": "sen ASG SFG S H D GD FJDF JDF J GFJ FDFGse2",
"score" : 1
}
]

我不明白 JSON 响应有什么问题。

【问题讨论】:

  • 我想指出,ng-repeat 应该在项目项目之上的层次结构中,它们永远不会以这种方式显示,将它们向下移动一级

标签: json angularjs rest


【解决方案1】:

您的 HTML 有很多错别字,以下格式正确:

<md-list>
    <md-item data-ng-repeat="item in sensesscores track by $index">
       <md-item-content>
         <div class="md-tile-content">
            {{item.sense}}
         </div>
         <div class="md-tile-left">
            {{item.score}}
         </div>
       </md-item-content> // this tag was not closed, but rather a new opener
   </md-item>
</md-list> // this tag was not closed properly, missing ">"

更改它,您的数据将按您的预期显示。

【讨论】:

    【解决方案2】:

    我想出了解决办法。需要将内容转换为 json 对象,我使用 jquery - $.parseJSON(data)

    【讨论】:

      猜你喜欢
      • 2021-10-14
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 2018-10-19
      • 1970-01-01
      • 2021-04-29
      • 2018-05-15
      相关资源
      最近更新 更多