JSON.stringify,更改密钥的大小写 [英] JSON.stringify, change the case of the key

查看:191
本文介绍了JSON.stringify,更改密钥的大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个返回json的web服务,并将json存储在一个局部变量中。 json代表一个简单的业务对象,例如:

I'm consuming a web service that returns json, and storing the json in a local variable. The json represents a simple business object such as:

var entry = {
  "FirstName": "John",
  "LastName": "Doe",
  ....
};

外壳是这样的,因为它与.net类中的属性名称匹配,按照我们的命名约定。

The casing is like that because it matches up with the property names from the .net class, as per our naming convention.

当更改其中一些属性并传回json时,Web服务现在需要驼峰大小写(再次,根据我们的方法命名约定)参数)而不是最初返回的pascal案例。

When a change a few of these properties and pass back the json, the web service now expects camel case (again, as per our naming convention for method parameters) instead of the pascal case initially returned.

var entry = {
  "firstName": "John",
  "lastName": "Doe",
  ....
};

这当然不起作用。

我正在使用 JSON.stringify 将json作为字符串发送回Web服务,我正在寻找一种简单的方法来更改骆驼案件的关键。但是,看起来我只能使用replacer参数来处理该值。

I'm using JSON.stringify to send the json back to the web service as a string, and I was looking to see if there was an easy way to change the key to camel case. However, it looks like I can only use the replacer param to work with the value.

我可以更改类的序列化,但是假设这不是一个选项。有什么想法?

I could change the serialization of the class, but lets pretend that's not an option. Any ideas?

谢谢。

推荐答案

你可以使用JSON替换器在写入之前切换键。

You can use a JSON replacer to switch keys before writing.

JSON.stringify(myVal, function (key, value) {
  if (value && typeof value === 'object') {
    var replacement = {};
    for (var k in value) {
      if (Object.hasOwnProperty.call(value, k)) {
        replacement[k && k.charAt(0).toLowerCase() + k.substring(1)] = value[k];
      }
    }
    return replacement;
  }
  return value;
});

相反,你可以使用JSON reviver。

For the opposite, you can use a JSON reviver.

JSON.parse(text, function (key, value) {
    if (value && typeof value === 'object')
      for (var k in value) {
        if (/^[A-Z]/.test(k) && Object.hasOwnProperty.call(value, k)) {
          value[k.charAt(0).toLowerCase() + k.substring(1)] = value[k];
          delete value[k];
        }
      }
      return value;
    });

第二个可选参数是一个函数,调用每个值作为解析的一部分创建或每个即将写的价值。这些实现简单地遍历键和小写的任何具有大写字母的字母的第一个字母。

The second optional argument is a function that is called with every value created as part of the parsing or every value about to be written. These implementations simply iterate over keys and lower-cases the first letter of any that have an upper-case letter.

http://json.org/js.html


可选的reviver参数是一个函数,将在最终结果的每个级别为每个键和值调用。每个值都将被reviver函数的结果替换。这可用于将通用对象重构为伪类的实例,或将日期字符串转换为Date对象。

The optional reviver parameter is a function that will be called for every key and value at every level of the final result. Each value will be replaced by the result of the reviver function. This can be used to reform generic objects into instances of pseudoclasses, or to transform date strings into Date objects.

stringifier方法可以采用可选的replacer函数。它将在结构中的每个值上的toJSON方法(如果有)之后调用。它将作为参数传递每个键和值,并且这将绑定到持有该键的对象。返回的值将被字符串化。

The stringifier method can take an optional replacer function. It will be called after the toJSON method (if there is one) on each of the values in the structure. It will be passed each key and value as parameters, and this will be bound to object holding the key. The value returned will be stringified.

这篇关于JSON.stringify,更改密钥的大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆