reviver-function

How to use JSON.parse reviver parameter to parse date string

試著忘記壹切 提交于 2019-12-17 18:30:43
问题 My JSON string contains a date field that returns such a value: "2009-04-04T22:55:16.0000000-04:00" I am particularly interested in parsing only the date compartment not the time. I tried using a reviver function, but interestingly the reviver function is never invoked! (tried on Firefox) Here is my code to accomplish that: var Site = { ..... dateReviver: function(key, value) { var a; if (typeof value === 'string') { a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec

JSON.parse reviver function has n+1 keys?

六月ゝ 毕业季﹏ 提交于 2019-12-13 01:35:10
问题 I wanted to test the code overload which can provide a reviver function when parsing a JSON string. So this code: JSON.parse('{"p": 5}', function(k, v) { if (k === "") return v; return v * 2; }).p; yields 10 (ok). But then I asked myself, 'what is this if (k === "") thing?' Lets remove it!: JSON.parse('{"p": 5}', function(k, v) { return v*2;}).p; //undefined !! Maybe because 5 is an integer? Let's try with parseInt : JSON.parse('{"p": 5}', function(k, v) { return parseInt(v)*2;}).p; /

node.js JSON.parse reviver doesn't report duplicate keys?

纵然是瞬间 提交于 2019-12-11 11:42:10
问题 I'm trying to use a JSON parser which will detect and save duplicate keys. I'm using JSON.parse() in node.js with a reviver, which I am hoping will tell me when I'm getting a duplicate key. However it doesn't. Is there another way? Is there a better JSON parser which handles duplicate keys in a reviver or other argument? "use strict"; try { var content = '{"value": "a", "value": "b", "value": "c" }'; console.log(content); var parsed = JSON.parse(content, function(k, v) { console.log(k+"="

How to create a map of records from a javascript raw object with Immutable.js?

点点圈 提交于 2019-12-08 15:59:38
问题 I'm new to immutable.js and I'd like to understand better how to use records starting from a raw JS object. With Immutable.fromJS() I can create a map passing a raw object, for example: var images = { "1": { id: "1", urls: ["/medium/1.jpg", "/large/1.jpg"] }, "2": { id: "2", urls: ["/medium/1.jpg", "/large/1.jpg"] } } var imagesMap = Immutable.fromJS(images); imagesMap is now a map containing other maps, one for each image. I'd like instead to create a map containing records, for example

Using a JSON.parse reviver to obfuscate fields

独自空忆成欢 提交于 2019-12-03 15:01:09
问题 I am attempting to abuse a reviver function with JSON.parse. I basically want to make certain fields "null". If I do this: var json_data = JSON.parse(j, function(key, value) { if (key == "name") { return value; } else { return null; } }); The entire json_data object ends up null. In fact, no matter what I make the else, that defines the value of the json_object. Interestingly, this works as expected: var json_data = JSON.parse(j, function(key, value) { if (key == "name") { return "name"; }

Javascript: Using reviver function, I seem can't get to alter all the keys, while concating the numbers

霸气de小男生 提交于 2019-11-29 16:49:27
I just want to change all the keys in batchesX. But I can't seem to alter all keys, because of concat. This is what I learned from post . Please advise how I can change all keys with numbers. var batchesX = '[{"batch":"0010002033"},{"batch":"0010001917"},{"batch":"0000020026"},{"batch":"0000017734"},'+ '{"batch":"0000015376"},{"batch":"0000014442"},{"batch":"0000014434"},{"batch":"0000014426"},'+ '{"batch":"0000013280"},{"batch":"0000012078"},{"batch":"0000012075"},{"batch":"0000012072"},'+ '{"batch":"0000011530"},{"batch":"0000011527"},{"batch":"0000011342"},{"batch":"0000010989"},'+ '{"batch

Javascript: Using reviver function, I seem can't get to alter all the keys, while concating the numbers

二次信任 提交于 2019-11-28 11:31:29
问题 I just want to change all the keys in batchesX. But I can't seem to alter all keys, because of concat. This is what I learned from post. Please advise how I can change all keys with numbers. var batchesX = '[{"batch":"0010002033"},{"batch":"0010001917"},{"batch":"0000020026"},{"batch":"0000017734"},'+ '{"batch":"0000015376"},{"batch":"0000014442"},{"batch":"0000014434"},{"batch":"0000014426"},'+ '{"batch":"0000013280"},{"batch":"0000012078"},{"batch":"0000012075"},{"batch":"0000012072"},'+ '{