簡體   English   中英

我如何使用angularjs($ http.get)根據在JSON文件中輸入的特定屬性解析json數據?

[英]How would i parse the json data according to the particular property entered in JSON file using angularjs($http.get)?

我有如下的JSON文件

{
  "colorsArray":[{
    "colorName":"red",
    "hexValue":"#f00"
  },
  {
    "colorName":"green",
    "hexValue":"#0f0"
  },
  {
    "colorName":"blue",
    "hexValue":"#00f"
  },
  {
    "colorName":"cyan",
    "hexValue":"#0ff"
  },
  {
    "colorName":"magenta",
    "hexValue":"#f0f"
  },
  {
    "colorName":"yellow",
    "hexValue":"#ff0"
  },
  {
    "colorName":"black",
    "hexValue":"#000"
  }
]}

以及我的js文件,具體如下:.....我正在使用$http.get功能讀取json文件

var app = angular.module('app', ['ngTagsInput']);
app.controller('MainCtrl', function($scope, $http) {

  $scope.loadTags = function(query) {
      return $http.get('tags.json');
  };
});

當使用AngularJS在$http.get外部引用JSON文件時,如何僅將colorName作為解析元素?

對json文件發出請求時,您不能即時查詢json file 一種方法和最有效的方法是將帶有查詢的HTTP調用作為參數發送到后端服務方法,並從服務器端執行並返回結果。

另一種方法是從客戶端過濾結果。 這樣,所有json數據都將作為響應返回,然后需要從控制器中過濾相關數據'

$scope.loadTags = function(query) {
    return $http.get('tags.jon').then(function(response) {
        var data = response.data.map(function(obj) {
            return obj.colorName;
        })
        return data;
    })
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM