首页 > 解决方案 > JSON字符串中的插值c#

问题描述

 Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
 Console.Write(unixTimestamp);
 var currentTimeUn = unixTimestamp.ToString();
string json = "{ \"Method\": \"GetShippedOrders\",\"Params\": { \"DateMin\": 1575158400, \"DateMax\": {currentTimeUn} }}";

如何将 currentTimeUn 传递给 DateMax?

标签: c#unixinterpolation

解决方案


使用字符串插值的解决方案

Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
var currentTimeUn = unixTimestamp.ToString();
string json = $"{{ \"Method\": \"GetShippedOrders\",\"Params\": {{ \"DateMin\": 1575158400, \"DateMax\": {currentTimeUn} }}}}";
Console.WriteLine(json);

推荐阅读