简体   繁体   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)?

I have my JSON file as follows 我有如下的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"
  }
]}

And my js file for the particular as follows.....I'm reading the json file here by using $http.get functionality 以及我的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');
  };
});

How will I be able to get only colorName as parsed element when the JSON file referred externally in $http.get using AngularJS? 当使用AngularJS在$http.get外部引用JSON文件时,如何仅将colorName作为解析元素?

you can't query the json file on the fly when the request made to json file . 对json文件发出请求时,您不能即时查询json file One way and most efficient way is to send http call with query as parameter to backend service method and execute and return the result from server side. 一种方法和最有效的方法是将带有查询的HTTP调用作为参数发送到后端服务方法,并从服务器端执行并返回结果。

Other way is filter the result from the client side. 另一种方法是从客户端过滤结果。 This way all the json data will return as the response and then need to filter the relevant data from the controller ' 这样,所有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