我在下面有一个多行字符串:如何使用Javascript将其转换为JSON?
git fetch
git checkout myVersion
git rebase origin/feature/version-1
git push --force
答案 0 :(得分:0)
要在JSON中格式化新行,您只需要用\n
特殊字符替换它们。如果要将文本转换为JSON格式,可以使用
JSON.stringify()
功能
jsonData = JSON.stringify(`Q1. This is the tile of the question
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
Ans. E
Q2. This is the tile of the question
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
Ans. A`);
结果将是:
"Q1. This is the tile of the question\nA. Option A\nB. Option B\nC. Option C\nD. Option D\nE. Option E\nAns. E\n\nQ2. This is the tile of the question\nA. Option A\nB. Option B\nC. Option C\nD. Option D\nE. Option E\nAns. A"
如果您想要一种更结构化的方式来存储这些数据(问题,选项和答案),您可以使用这样的JSON结构:
[
{
"question": "Question 1",
"a": "Option A",
"b": "Option B",
"c": "Option C",
"d": "Option D",
"answer": "c"
},
{
"question": "Question 2",
"a": "Option A",
"b": "Option B",
"c": "Option C",
"d": "Option D",
"answer": "b"
}
]