0

I trying to convert a Table to JSON using Newtonsoft The main problem I have is that the values comes in different data types, so the INT does not return as a string.

Is there a simple way to do it? or do I have to look at the examples where you create custom converter?

dynamic data = table.CreateDynamicInstance();

var jsonBody = Newtonsoft.Json.JsonConvert.SerializeObject(data);

_settings.Request.AddParameter("application/json", jsonBody, ParameterType.RequestBody);

jsonBody returns...

{"phone":555555,"email":"john.doe@svea.com"}

but should return...

{"phone":"555555","email":"john.doe@svea.com"}

Thanks in advance

Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
Dymond
  • 2,158
  • 7
  • 45
  • 80
  • 2
    If you want to return strings, set the type to string...? – Heretic Monkey Apr 01 '19 at 18:08
  • @HereticMonkey Yes, I tried that but it doesn't work – Dymond Apr 01 '19 at 18:09
  • Does this [answer](https://stackoverflow.com/a/39526179/4685428) helps? – Aleks Andreev Apr 01 '19 at 18:10
  • 1
    "_Is there a simple way to do it? or do I have to look at the examples where you create custom converter?_" Well, unless you can make `table.CreateDynamicInstance();` return objects with string fields/properties instead of numeric fields/properties, you have to use a custom JsonConverter... –  Apr 01 '19 at 18:12
  • Please show what you've tried, because if the type of a property is `string`, `JsonConvert` will output it as a quoted string. – Heretic Monkey Apr 01 '19 at 18:12
  • 1
    That said (as addition to my former comment), i feel that using a custom JsonConverter would be better in any case. Why trying to change the behavior of `table.CreateDynamicInstance();` (which might possibly have further repercussions to other parts of your code working with the table and/or CreateDynamicInstance method) just to accomodate the Json serializer? That doesn't make sense to me, since you can just keep this Json-related problem and its solution/fix local to the Json serialization process by using a custom JsonConverter... –  Apr 01 '19 at 18:17
  • @elgonzo yes thanks, it seems like that's the way to go! – Dymond Apr 01 '19 at 18:18

0 Answers0