添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
js中json动态设置key的值var obj={};obj.name="object";obj["age"] =26;var key="keyString";obj[key]="keyValue";
在开发 ,后台传给前端的数据结构都是固定的,前端只要根据固定的 key 来获取对象 的数据 就可以了。 有时候页面需要对象数据也只需要最简单的定义对象属性的方法就可以了。比如说: var obj = {} // 第一种方法 obj.name = 'css js .cn' // 第二种方法 obj['age'] = 99 obj['address'+'work'] = '南京' 这里要说一下第三种方法,也是es6 支持的一种方法,但现在对于开发方式众多前端开发来说,es6的语法已经可以用在了开发当 了。 JSON .stringify(value, replacer, space) value any JavaScript value, usually an object or array. replacer an optional parameter that determines how object values are stringified for objects. It can be a function or an array of strings. space an optional parameter that specifies the indentation of nested structures. If it is omitted, the text will be packed without extra whitespace. If it is a number, it will specify the number of spaces to indent at each level. If it is a string (such as '\t' or ' '), it contains the characters used to indent at each level. This method produces a JSON text from a JavaScript value. When an object value is found, if the object contains a to JSON method, its to JSON method will be called and the result will be stringified. A to JSON method does not serialize: it returns the value represented by the name/value pair that should be serialized, or undefined if nothing should be serialized. The to JSON method will be passed the key associated with the value, and this will be bound to the value For example, this would serialize Dates as ISO strings. Date.prototype.to JSON = function ( key ) { function f(n) { // Format integers to have at least two digits. return n < 10 ? '0' + n : n; return this.getUTCFullYear() + '-' + f(this.getUTCMonth() + 1) + '-' + f(this.getUTCDate()) + 'T' + f(this.getUTCHours()) + ':' + f(this.getUTCMinutes()) + ':' + f(this.getUTCSeconds()) + 'Z'; You can provide an optional replacer method. It will be passed the key and value of each member, with this bound to the containing object. The value that is returned from your method will be serialized. If your method returns undefined, then the member will be excluded from the serialization. If the replacer parameter is an array of strings, then it will be used to select the members to be serialized. It filters the results such that only members with key s listed in the replacer array are stringified. Values that do not have JSON representations, such as undefined or functions, will not be serialized. Such values in objects will be dropped; in arrays they will be replaced with null. You can use a replacer function to replace those with JSON values. JSON .stringify(undefined) returns undefined. The optional space parameter produces a stringification of the value that is filled with line breaks and indentation to make it easier to read. If the space parameter is a non-empty string, then that string will be used for indentation. If the space parameter is a number, then the indentation will be that many spaces. Example: text = JSON .stringify(['e', {pluribus: 'unum'}]); // text is '["e",{"pluribus":"unum"}]' text = JSON .stringify(['e', {pluribus: 'unum'}], null, '\t'); // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]' text = JSON .stringify([new Date()], function ( key , value) { return this[ key ] instanceof Date ? 'Date(' + this[ key ] + ')' : value; // text is '["Date(---current time---)"]' JSON .parse(text, reviver) This method parses a JSON text to produce an object or array. It can throw a SyntaxError exception. The optional reviver parameter is a function that can filter and transform the results. It receives each of the key s and values, and its return value is used instead of the original value. If it returns what it received, then the structure is not modified. If it returns undefined then the member is deleted. Example: // Parse the text. Values that look like ISO date strings will // be converted to Date objects. myData = JSON .parse(text, function ( key , value) { var a; if (typeof value === 'string') { /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value); if (a) { return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6])); return value; myData = JSON .parse('["Date(09/09/2001)"]', function ( key , value) { var d; if (typeof value === 'string' && value.slice(0, 5) === 'Date(' && value.slice(-1) === ')') { d = new Date(value.slice(5, -1)); if (d) { return d; return value; This is a reference implementation. You are free to copy, modify, or redistribute.
strtest = { 故宫:好地方,天涯:北京} print strtest #####{'\xe4\xb8\xad\xe6\x95\x85\xe5\xae\xab': '\xe5\xa5\xbd\xe5\x9c\xb0\xe6\x96\xb9', '\xe5\xa4\xa9\xe6\xb6\xaf': '\xe5\x8c\x97\xe4\xba\xac'} strtestObj = json .loads( json .dumps(strtest)) print >>strtestObj ,str
标题比较麻烦,都有些叙述不清;昨天下午在调试接口框架的时候,遇到了一个问题是这样的: 使用python 写了一个函数,return 了两个返回 比如 return a,b 于是返回的a,b 是tuple类型,比如 是actual。那么,得到a,b分别是actual[0] ,actual[1]这样的。而目前,actual[0]的 是这样的: {“code”:”m0001”,”result”:True} ,但是我想得到code的 key m0001 ,那么这里,我该使用那样的代码取到 m001 ? 这里几个问题,首先 需要将tuple类型转化为str类型,再转化为dict类型,然后提取 json 格式
前言:前两天遇到这个业务需要,需要将一个 json 对象 的键( key )改变为特定的键,便自己在科普后做了如下功能。 实现思路如下: 先讲 json 对象使用JOSN.stringify()转换为字符串,然后使用字符串的replace功能替换掉需要改变的键( key )的字符串. 代码实现如下: 第一种,每个 key 的改变的规律相同的情况,(下例为每个 key 去掉前两个字符作为新 key ) var array = [ myid:1, myname:"小明"
有时候我们会面临着需要把 js 的变量作为 json key 使用的情况,但是却往往遇到 js 执行时把变量名而不是变量 作为了 key 。 具体解决描述如下. 假使 var key 1 = "aaa"; var value1 = "bbbb"; json 对象 data={k:'aa',b:'aaa'}; 这时如果想给data改为 {k:'aa',b:'aaa',aaa:'bbbb'};
python自带的 json 包能够方便的解析 json 文本,但是如果 json 文本 包含重复 key 的时候,解析的结果就是错误的。如下为例 {" key ":"1", " key ":"2", " key ":"3", " key 2":"4"} 经过解析,结果却如下所示: " key ":"3", " key 2":"4" 原因是python解析的时候是创建一个字典,首先会读取到 key ,但是后面遇到重复键的时候,后来的 会覆盖原来的 ,导致最后只有一个 key 留下来。 这肯定不是我们想要的结果,其 一种结果可以是将相同键的 聚合成一个数组,即如下所示。 " key ":["1","2","
var smsTypeDesc = {"4":"回访短信","3":"邮件短信","aa":"测试短信"}; function Enuma Key (){ for(var key in smsTypeDesc){ let myMap = new Map(); myMap.set(" key 1", {name: "张三", age: 20}); myMap.set(" key 2", {name: "李四", age: 25}); // 使用forEach方法遍历Map JSON 对象 myMap.forEach(function(value, key ) { console.log( key + " : " + JSON .stringify(value)); // 使用for...of循环遍历Map JSON 对象 for(let [ key , value] of myMap) { console.log( key + " : " + JSON .stringify(value)); 【问题已解决】The server time zone value '?й???????' is unrecognized or represents more than one time zone 41209