简体   繁体   中英

Writing beautiful JSON using multiple line strings?

Context

For personal use, im creating a small project that documents my learning about data structures. I want to dynamically pull strings from a JSON file into a custom Vue component that displays code that can be run and edited in the browser by the user.

The Probem

json图片

I find the above image far easier to, not only type, but also to read, than something like this (including variations including escape strings on a single line):

   "BasicArithmic" : "var a = 1;var b = 2;var c = a + 5*b;return c;"

Ultimately i just want to be able to type the string out as multiple lines inside the code so that i'm not wasting my time trying to work out what code is supposed to do.

Clarification

Im passing in code as a string to an eval(), since i couldn't work out how to pass code between components and THEN convert the code to a string.

Use ` instead of "

 console.log(` a = 1; b = 2; ` )

Or you could add \ to the end of each line:

 x = { "BasicArithmic": "\ var a = 1;\ var b = 2;\ var c = a + 5*b;\ return c;\ "} console.log(x.BasicArithmic);

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