0

I have a json object

{
  "userId":1,
  "birthDateTime":"1999-08-26T01:38:42Z",
  "propNameN":null
}

I want to convert non nullable props to query params

var sb = new StringBuilder();
foreach (var c in json.Children().Cast<JProperty>())
   {
      if (!string.IsNullOrWhiteSpace(c.Value.ToString()))
         {  
            //birthDateTime here has already changed format => {08/26/1999 01:38:42}
            sb.Append($"&{c.Name}={HttpUtility.UrlEncode(c.Value.ToString())}");         
         }
    }

at the moment result is

userId=1&birthDateTime=08%2f26%2f1999+01%3a38%3a42

but is should be

userId=1&birthDateTime=1999-08-26T01%3a38%3a42Z
dbc
  • 104,963
  • 20
  • 228
  • 340
Alex
  • 8,908
  • 28
  • 103
  • 157
  • Is that birthDateTime line a typo? Is the `:` really inside the double quote? – Broots Waymb Aug 26 '21 at 14:01
  • @BrootsWaymb yes, fixed – Alex Aug 26 '21 at 14:02
  • 2
    `DateTime` values don't have a format. The current `CultureInfo` has a format associated with it that is used when you call `ToString()`. So give that a different format to use (e.g., `ToString("o")` – Heretic Monkey Aug 26 '21 at 14:11
  • @HereticMonkey I already set correct format that I need previously, now I just want to get all json properties that are not null and create query properties id=1&birthDateTime=1999-08-26T01%3a38%3a42Z, but JProperty detect this field as date time and format is changed, maybe is a way to create JProperty without detection of the datetime – Alex Aug 26 '21 at 14:15
  • Okay, if you want to do it the hard way, have at it. Or you could just pass a format string to `ToString` with the format you want. – Heretic Monkey Aug 26 '21 at 14:35
  • @HereticMonkey very similar, thx for you time – Alex Aug 26 '21 at 14:58

0 Answers0