Md Abdul Halim Rafi
Md Abdul Halim Rafi

Reputation: 1970

How to format a multi line string with triple quotes inside in JSON file?

I have markdown string to store in json file, so I am using multi line and so many things in the string. How can I store it in json object?

[
  { "title":  "Title of Ebook",
    "details": [
      {
        "head": "Introduction 1",
        "data": """It’s important to follow trends in your field to ensure you’re staying current on standards and protocols and perhaps more so in the field of coding. Programmers of all specialties can benefit from following 
#h this is a header
__italic__
industry-leading blogs to stay aware of the latest technologies.If you’re a coder of any sort you’ll want to subscribe to these useful programming blogs written by the top blogging coders.Each of these bloggers has made a name for themselves in the programming community by sharing relevant, high-quality information and tips for coders. They maintain their respective blogs well and keep current information posted on a regular basis.By following the best programming blogs you’ll find tips and shortcuts you may never have otherwise thought to try. Consider using an RSS feed reader through your phone or desktop browser to automatically download each new post from these top coding bloggers."""
      }
  }
]

When I will receive the string I will have the markdown style data from json.

Upvotes: 4

Views: 5528

Answers (1)

SebastianK
SebastianK

Reputation: 782

Your object is not closed correctly. There are closing brackets missing.

Not sure if it answers your question but you can do it like this:

import 'dart:convert';

main(List<String> arguments) {
  var json = [
    {
      "title": "Title of Ebook",
      "details": [
        {
          "head": "Introduction 1",
          "data":
              """It’s important to follow trends in your field to ensure you’re staying current on standards and protocols and perhaps more so in the field of coding. Programmers of all specialties can benefit from following 
#h this is a header
__italic__
industry-leading blogs to stay aware of the latest technologies.If you’re a coder of any sort you’ll want to subscribe to these useful programming blogs written by the top blogging coders.Each of these bloggers has made a name for themselves in the programming community by sharing relevant, high-quality information and tips for coders. They maintain their respective blogs well and keep current information posted on a regular basis.By following the best programming blogs you’ll find tips and shortcuts you may never have otherwise thought to try. Consider using an RSS feed reader through your phone or desktop browser to automatically download each new post from these top coding bloggers."""
        }
      ]
    }
  ];

  var encodedJson = jsonEncode(json);

  print(encodedJson);

  var details = json[0]['details'] as List;
  print(details[0]['data']);
}

Upvotes: 6

Related Questions