简体   繁体   中英

Auto Convert Json String to C# Object

I have this string below with '\\t', '\\r' and '\\n' which I need to convert to a valid Json format with a loop so that I can Deserializeto an object without listing the object properties.

"Title\\tFirstName\\tLastName\\tAge\\r\\nMr\\tBla bla\\tBla bla\\t25\\r\\nMiss\\tBla bla\\tBla bla\\t35\\r\\n"

You can use String.Split method to solve your problem.

  • First of all you need to split by \\r\\n - this will give you individual rows with data
  • You can loop through these rows and split each of them by \\t symbol - this will give you array of properties
  • After you have all the "ingredients" - you can build your objects: using dynamic objects new { firstName = arrayData[0], lastName = arrayData[1], ..} , or you can create a new class with all the required properties
  • Last step will be serialize collection of your objects into json - I would recommend Json.NET library for this purpose: http://www.newtonsoft.com/json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM