Date determination in json file

I am interested to know how to set today's date in json file, as we use in js.

Is it possible to specify Date.today () in a json file?

Because json data has a date object that indicates the system date whenever we read a json file.

I hope you understand what I'm trying to say.

Thanks in advance,

-Raja.

+7
source share
8 answers

JSON is a structured transport format. There is no logic in it.

But here are the options:

  • Why not just get the date when you read the file?

  • Does the server create this JSON, which includes the date on which it was generated. However, this is not ideal if you need a current date. When you read the file, the date generated by the server has already passed.

  • Build a parser that parses the string and searches for custom markup.

For example , special markup is contained in @{} . Get the command inside, define the command and do the replacement.

 var jsonString = '{"data1" : "foo","date" : "@{currentdate}"}' 

In this case, I will find @{currentdate} . I have to create a function corresponding to this command to replace @{currentdate} with the current date while reading (in the required format)

 var parsedString = jsonString.replace(/@\{(\w+)\}/g, function(match, group) { if (group === 'currentdate') { return new Date(); } else if (group === 'anotherCommand') { return 'anotherValue'; } //and so on }); 

and the result will be like this:

 jsonString = '{"data1" : "foo","date" : "Fri May 04 2012 01:17:07 GMT-0700 (PDT)"}' 
+8
source

The server side can generate JSON dates in ISO format, for example, "2012-04-30T02: 15: 12.356Z" Then the client side can parse and load the dates into the object

 new Date(Date.parse("2012-04-30T02:15:12.356Z")) 
+10
source

I suggest you use JSON-js ( json2.js ) as it parses all standard JSON, but also allows you to add parsing logic called the reviver function, which is very well suited to your scenario. The basic syntax for starting a JSON parser using a custom handler is as follows:

var myObject = JSON.parse(myJSONtext, reviverFunction);

Using your input example as a guide, you can configure it this way:

 var jsonTxt = '[{' + '"data1": "foo",' + '"Date": "",' + '"childs": [{' + '"data2": "stack",' + '"Date": ""' + '}{}{}...]}]'; //and-on-and-on as in your comment myData = JSON.parse(jsonTxt, function ( key, value ) { if ( key === 'Date') { return new Date(); } //any additonal custom logic you may need later... }); 

A general introduction to JSON-js is provided in JSON on the JavaScript page , and a brief intro about JSON and the page also includes some usage scenarios.

+4
source

Wouldn't it be easier to just calculate the current system data whenever you read a file? I may have a lack of context, but I see no reason to store this in a document.

If you really need to do this, you can do the following:

 var jsonObject = {"now":"new Date()"}; var date = eval(jsonObject.now); 
+1
source

If you want to save the date, I would rather store it as a string with a format like yyyy / mm / dd hh: mm: ss or something like that, and parse it in a Date object when I want to read it on the right my tongue.

 obj = { dates : ['2012/04/30 10:14:23', '2012/05/01 05:34:01'] } 

I don’t understand exactly what you want using the eval methods (this is an ugly practice), you can add a method that puts the actual date in the object, and also adds itself to the children and calls the method added to the children.

 obj = { data : "foo", addDate : function() { this.date = newDate(); if (this.children) { for (var i = 0; i < this.children.length; i++) { this.children[i].addDate = this.addDate; this.children[i].addDate(); } } }, children : [{ data : "foo2" }] } 

PS, if you want to save it in a file, then you should use the eval method (not recommended) that stores the methods as a string or file download time.

 jsonLoaded; // this var has the json that you store in a file; var addDate = function() { this.date = newDate(); if (this.children) { for (var i = 0; i < this.children.length; i++) { this.children[i].addDate = this.addDate; this.children[i].addDate(); } } this.addDate = null; // remove the function we have added // delete this.addDate; // if is compatible with browser } jsonLoaded.addDate = addDate; jsonLoaded.addDate(); 

you cannot contract json objects with functions, because of this, in the second method, after adding the addDate method, we remove this from the json object (you can also remove this.addDate, but I don’t know, it works in IE)

+1
source

You might consider using a popular library like moment.js http://momentjs.com/

Then you can save the date in YYYY-MM-DD format in json and let it parse it:

 var dateString = '2012-11-01'; var someday = moment(dateString); var formattedDate = someday.format('ddd, DD MMM YYYY'); // 'Thu, 01 Nov 2012' 
+1
source

it will be useful to collect all the dates you want to transfer to

 Collection<String> dates = new ArrayList<String>(); 

Convert this collection to a json object, and then convert it back to the end of the receiver. You can use the joda date API for conversions.

0
source

I use Gson in java to generate json output, but Gson does not allow me to put javascript functions in json. So here is what I am doing: use replacement tags for the places where you want to put the code (for example, one of the early answers). Then get the json text, replace the tags and save the text in a json file:

  Map<String, String> dynamicDates = new HashMap<>(); dynamicDates.put("d1", "new Date()"); dynamicDates.put("d2", "new Date(2015, 0, 1, 9, 30)"); dynamicDates.put("d3", "new Date(2015, 0, 1, 12, 30)"); JsonObject json = new JsonObject(); JsonObject root = new JsonObject(); JsonObject level_1_A = new JsonObject(); JsonObject level_1_B = new JsonObject(); json.add("root", root); root.add("level_1_A", level_1_A); root.add("level_1_B", level_1_B); level_1_A.addProperty("d1", "${d1}"); level_1_A.addProperty("d2", "${d2}"); level_1_B.addProperty("d3", "${d3}"); StringBuilder sb = new StringBuilder(); new GsonBuilder().setPrettyPrinting().create().toJson(json, sb); String str = sb.toString(); for (String key : dynamicDates.keySet()) { str = str.replace("\"${" + key + "}\"", dynamicDates.get(key)); } String jsonText = str; String javascriptText = "var myJson = " + str + ";"; System.out.println(jsonText); System.out.println(javascriptText); 

Thus, there is nothing left to do on the consumption side when using this json. And the first conclusion:

  { "root": { "level_1_A": { "d1": new Date(), "d2": new Date(2015, 0, 1, 9, 30) }, "level_1_B": { "d3": new Date(2015, 0, 1, 12, 30) } } } 

My use of json usually saves it as job javascript, so this works for me.

0
source

All Articles