0

I'm trying to convert a txt to json fomat in Javascript,if I put the txt in 1 line as following code ,it works good:

<html>
<body>

<p id="demo"></p>

<script>
const txt = '{"name":"John", "age":30, "city":"New York","home":"yes"}'
const obj = JSON.parse(txt);
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age;
</script>

</body>
</html>

However in my real business ,the txt is very long ,and I have to start from new line ,I added '/n' :

<!DOCTYPE html>
    <html>
    <body>
    
    <p id="demo"></p>
    
    <script>
    const txt = '{"name":"John", "age":30, "city":"New York", + "/n" +
   "home":"yes"}'
    const obj = JSON.parse(txt);
    document.getElementById("demo").innerHTML = obj.name + ", " + obj.age;
    </script>
    
    </body>
    </html>

But it not works,any friend can help ?

William
  • 3,724
  • 9
  • 43
  • 76

1 Answers1

0

Try using escape character like this:

const txt = '{"name":"John", "age":30, "city":"New York",\n' +'"home":"yes"}';
semih
  • 45
  • 1
  • 8