convert date to json date format c# - json

Convert date to json date format c#.
I have a date format 21-01-2015. I need to change this date to 1326067200000.
string date="21-01-2015";
I need output-1326067200000

JavaScriptSerializer ser = new JavaScriptSerializer();
var json = ser.Serialize(DateTime.Now);

Related

Passing date in json file in KarateFramwork

I have following function to get current date:
function() {
var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
var sdf = new SimpleDateFormat('yyyyMMdd');
var date = new java.util.Date();
return sdf.format(date);
}
And I'm passing the date to JSON file as follows:
* def currDate = getDate()
JSON File:
{
"clientId": "ABC",
"serviceLine": "DSS",
"locationId": "VOL",
"serviceType": "ADC",
"attendanceDate": #(currDate),
"saId": "123",
"attendance": "PRESENT",
"attendanceType": "ATTENDANCE"
}
Json by default converts date into string for e.g. "20230210" (yyyyMMdd). This format is fine & as expected. But just I pass the variable currDate to JSON, it converts it into String with "", e.g., "20230210" but request should have date without quotes, i.e. just 20230210. Otherwise it gives error as Invalid Input.
How can I convert string to date format in JSON file?
After seeing your comments, looks like you are just trying to use the date format output as a number. That's easy, just do this:
* def currDate = 1 * getDate()
Also refer: https://github.com/karatelabs/karate#floats-and-integers

org.json.JSONObject converting number to exponential form

I am using org.json.JsonObject (20190722 version) to convert a string into a json object. It's converting the decimal numbers into exponential format.
String jsonString = "{\"value1\":12345678,\"value2\":\"Name\",\"value3\":123456789.345}";
JSONObject json = new JSONObject(jsonString);
I am expecting output:
"value2":"Name","value1":12345678,"value3":123456789.345
But I am getting:
"value2":"Name","value1":123456789,"value3":1.23456789345E8

Date Format in Json request/response of an API

I am having a Date field - startDate in my pojo.
In API request body in JSON I have to mention date in following format -
{
...
"startDate" : "2017-05-19T14:00:00",
...
}
But in the response I get the following format -
{
...
"startDate" : "2017-05-19 14:00:00",
...
}
There is no 'T' in the response JSON.
What should I do to make it consistent and have the 'T' in response as well.
You can use below code for yyyy-MM-dd'T'HH:mm:ss to yyyy-MM-dd HH:mm:ss at the time of request.
String time = "2017-05-19T14:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse(time);
String formattedTime = output.format(d);
System.out.println(formattedTime);
OR
you can use below code for yyyy-MM-dd HH:mm:ss to yyyy-MM-dd'T'HH:mm:ss at the time of response.
String time = "2017-05-19 14:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date d = sdf.parse(time);
String formattedTime = output.format(d);
System.out.println(formattedTime);
There two ways to achieve the result.
Use String in server side.So you can get the same result in both
request and response.
You could convert date object into Long(millisecond).
For your information, The T is just a literal to separate the date from the time.
You can use the below code
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")

Get NSDate from JSON?

I am working with Openweather API and trying to parse date property from JSON. This is date format: "dt" - Time of data calculation, unix, UTC. I have Forecast struct and there i've var date: NSDate. In weatherManager.swift i am working with SwiftyJSON and i tried with let date = json["dt"].double and other extension but it won't work.
Any suggestion? Thanks
If the date inside the API response is Unix timestamp, that means that you have to convert it to NSDate object. Since the unix time timestamp is number of seconds (in OpenWeather API) elapsed from 1. January 1970, we can use NSDate(timeIntervalSince1970:) method to convert it to NSDate object that you need.
let dateUnix = json["dt"].double
let date = NSDate(timeIntervalSince1970: dateUnix)
date object is now NSDate that you need.

solrj QueryResponse convert to json

I use solrj to submit a query to solr , that returns the result in json format.
SolrQuery query = new SolrQuery();
SolrQuery query = new SolrQuery();
query.setParam("kw", keyword);
query.setParam("lc", location);
query.setParam("wt", "json");
query.setParam(CommonParams.QT, "/lds");
QueryResponse qResponse = solrServer.query(query);
searchResultStr = qResponse.toString();
But the searchResultStr does not have a string in JSON format. Instead it has something like this:
{responseHeader={status=0,QTime=21},location={has_zipcode=true,location_param=94085}}
But if i directly hit the solr url in the browser, I get the correct JSBON format:
{"responseHeader":{"status":0,"QTime":15},"location": {"has_zipcode":true,"location_param":"94085"}}
For a JSON output you will have to query a HTTPSolrServer using curl as mentioned in the answer here. Using EmbeddedSolrServer will not help i.e. solrj.
When you use solrj to query you will have to get the SolrDocumentList from the QueryResponseobject and convert it into JSON format by iterating through each SolrDocument and entering the data into JSON the way you want.
Solr QueryResponse returns a hashmap, so use any hashmap to json converter to get the json out of it. Here I have used Gson() for that conversion.
QueryResponse response = solr.query(query);
Gson gson = new Gson();
String json = gson.toJson(response.getResults());
System.out.println(json);