Home > Software engineering >  Line Break In JSON String Javascript
Line Break In JSON String Javascript

Time:11-19

I'm trying to read data from a JSON file that contains a list of blogs. I want to break each of those blogs into small paragraphs. How can I achieve this?

Book Image

[
  {
    "id": 6,
    "name": "The 4-Hour Work Week",
    "blog": "The first chapter, D is for Definition, followed its name and defined the clear cut differences in mindset between the new rich and the deferrers. This clued me in to the goal of this book, to escape what has been perpetuated our entire lives. However, I was still skeptical. It presented several, grandiose claims; how could any of it be truly doable? Sure, someone like Tim Ferriss, Princeton graduate and energy-supplement extraordinaire could pull it off, but that doesn't mean that me, the average Joe, could somehow make an automated stream of revenue so that I can live all around the world and have no worries.The next chapter, E is for Elimination, had the most profound effect on me. It presented ideas such as Parkinson's Law (work expands to fill the time allotted to it) and the Pareto Principle (80/20), ideas I had heard from videos before, but never explained as well as in this chapter. After reading, I immediately evaluated my time wasters and consumers and began to slowly deconstruct my day and how to best optimize it. This chapter was one that I could apply the day after reading it successfully, so it was very useful. \\n\\ The next chapter, E is for Elimination, had the most profound effect on me. It presented ideas such as Parkinson's Law (work expands to fill the time allotted to it) and the Pareto Principle (80/20), ideas I had heard from videos before, but never explained as well as in this chapter. After reading, I immediately evaluated my time wasters and consumers and began to slowly deconstruct my day and how to best optimize it. This chapter was one that I could apply the day after reading it successfully, so it was very useful.",
    "img": "https://i.ibb.co/Pxtt4dH/4-hour-work-week.jpg"
  }
]

CodePudding user response:

Replace \\n\\ with \n to add a line break. Use \n\n to add two line breaks.

CodePudding user response:

You can add <p> tag for each needed place on the string then while reading JSON split it based on <p> tag and then create your own HTML.

example:

const json = "Paragraph 1<p>Paragraph 2";
const p = document.querySelector('#paragraph');

p.innerHTML = json.split('<p>').map(paragraph=>{
       return '<p>'   paragraph   '</p>';
      }).join('')
<html>
<body>
<div id="paragraph"> </div>
</body>
</html>

  • Related