大写与小写

时间:2018-06-28 10:50:04

标签: javascript html angularjs

我有一个简单的问题。

我有多种方法可以从我无法控制的其他应用程序获取JSON。所有键都具有相同的名称,但是有些键是大写的,有些是小写的。

我如何读取JSON,所以不管是小写还是大写?

例如在我的代码中,当json的customer_id小写时不起作用

HTML

 <td ng-repeat="customer in customers">{{customer.CUSTOMER_ID}}</td> // this only works when the JSON keys comes uppercase

控制器

// Get Customers
promises.customers.$promise.then(function (data) {
    $scope.customers = data["Customers"].Customers;

    if(!angular.isArray($scope.customers)) {
        $scope.customers = [$scope.customers];
    }
    console.log($scope.customers);
}.bind(this));

4 个答案:

答案 0 :(得分:1)

您可以将属性规范化为全部大写或小写:

// Customers with a sample customer as provided in some comment by the OP
const customers = [{
    "account_id": 1,
    "account_type_description": "Single Account",
    "customer_id": 1,
    "first_name": "Peter",
    "last_name": "Parker",
    "identity_card_number": 128,
    "tax_identification": 35,
    "birth_date": "2018-06-28T07:57:23Z",
    "customer_gender_description": "Male",
    "street_address": "Gotham Street 56",
    "postal_code": "21312",
    "city": "Gotham",
    "country_description": "Portugal"
  },
  {
    "ACCOUNT_ID": 1,
    "ACCOUNT_TYPE_DESCRIPTION": "Single Account",
    "CUSTOMER_ID": 1,
    "FIRST_NAME": "Peter",
    "LAST_NAME": "Parker",
    "IDENTITY_CARD_NUMBER": 128,
    "TAX_IDENTIFICATION": 35,
    "BIRTH_DATE": "2018-06-28T07:57:23Z",
    "CUSTOMER_GENDER_DESCRIPTION": "Male",
    "STREET_ADDRESS": "Gotham Street 56",
    "POSTAL_CODE": "21312",
    "CITY": "Gotham",
    "COUNTRY_DESCRIPTION": "Portugal"
  }
]

const normalizeProperties = entities => entities.map(customer => {
  const outputCustomer = {}

  for (let property of Object.keys(customer))
    outputCustomer[property.toLowerCase()] = customer[property]

  return outputCustomer
})

const normalizedCustomers = normalizeProperties(customers)

console.log(JSON.stringify(normalizedCustomers))

有很多方法可以得到相同的结果,但是我们的想法是,一旦将模型绑定到UI,就不希望这些差异。

答案 1 :(得分:0)

我会进行属性归一化,即可以使用mapKeys使用lodash:

const input = { CUSTOMER_ID: 1, name: 'foo' };
const normalized = _.mapKeys(input, (value, key) => key.toLowerCase());

// {customer_id: 1, name: "foo"}

console.log(normalized.customer_id); // 1

如果属性可以写为camelCase,则还可以使用snakeCase将其标准化:

const input = { CUSTOMER_ID: 1, otherId: 3, name: 'foo' };
const normalized = _.mapKeys(input, (value, key) => _.snakeCase(key));

// {customer_id: 1, other_id: 5, name: "foo"}

console.log(normalized.other_id); // 5

答案 2 :(得分:0)

要忽略JSON中所有键的大小写,您可以为以下对象定义自定义getter

Object.defineProperty(Object.prototype, "getProp", {
    value: function (prop) {
        var key,self = this;
        for (key in self) {
            if (key.toLowerCase() == prop.toLowerCase()) {
                return self[key];
            }
        }
    },
    enumerable: false
});

现在,您可以获得忽略这种情况的属性

obj.getProp("CUSTOMER_ID");
//or
obj.getProp("customer_id");

答案 3 :(得分:0)

您可以实现自己的解析函数,并在所有JSON响应上调用它以对其进行规范化。

例如将所有按键标准化为小写的功能

function getParsedObjectWithUpperCaseKeys(jsonObject) {
    var parsedObjectWithUpperCaseKeys = {};

    Object.keys(jsonObject).forEach(function (jsonObjectKey) {
        parsedObjectWithUpperCaseKeys [jsonObjectKey.toUpperCase()] = jsonObject[jsonObjectKey];
    });

    return parsedObjectWithUpperCaseKeys;
}

在您的控制器代码中:

// Get Customers
promises.customers.$promise.then(function (data) {
    var customers = data["Customers"].Customers;

    $scope.customers = getParsedObjectWithUpperCaseKeys(customers);

    if(!angular.isArray($scope.customers)) {
        $scope.customers = [getParsedObjectWithUpperCaseKeys(customers)];
    } else {
        $scope.customers = [getParsedObjectWithUpperCaseKeys(customers[0])];
    }
    console.log($scope.customers);
}.bind(this));