8

Possible Duplicate:
Removing all whitespace from a string efficiently
Minify indented JSON string in .NET

I have a string like this:

{"languages" : [{"fluency": 4, "id": 15}], "address" : {"city_id" : 8341, "city_name" : "My city"}, "about" : null, "birthday" : "1988-03-18", "email" : "email@a.com", "id" : 3, "income" : 4}

I would like to have a compact/minified string like this:

{"languages":[{"fluency":4,"id":15}],"address":{"city_id":8341,"city_name":"My city"},"about":null,"birthday":"1988-03-18","email":"email@a.com","id":3,"income":4}

NOTE:

  • I'm using the built in System.Json to perform serialization in my app. I retrieve the string representation of a JsonValue object using the ToString () method, but looks like I have no control over the format of the output string.
  • I'd like to use a helper method to 'minify' the JSON string. I don't want to include another third party Json library in the project.
  • I'm using complex JSON data structures (including nested objects/arrays)
  • I'm using Mono, not .NET
Community
  • 1
  • 1
Eduardo Coelho
  • 1,953
  • 8
  • 34
  • 60
  • 6
    Have a look at this question: [Minify indented JSON string in .NET](http://stackoverflow.com/questions/8913138/minify-indented-json-string-in-net) – k.m Jan 16 '13 at 18:51
  • 1
    @EricJ. I need to remove all whitespaces from a JSON string, not an arbitrary string. The final result would become `"address":{"city_id":8341,"city_name":"My city"}` (note the preserved space in `"My city"` – Eduardo Coelho Jan 16 '13 at 18:53
  • 2
    Best answer can be found here: http://stackoverflow.com/a/28456818/1912609. Code: string json = jObject.ToString(Formatting.None); – Orhan Sep 15 '16 at 05:39

0 Answers0