Javascript 如何使用php将json数组转换为java脚本?
我有这些数据Javascript 如何使用php将json数组转换为java脚本?,javascript,php,json,Javascript,Php,Json,我有这些数据 {"quiz": [{"question":"What is your favorite color?", "choices":[{"prefix":"A","content":"Red"},{"prefix":"B","content":"Blue"},{"prefix":"C","content":"Yellow"},{"prefix":"D","content":"Pink"}]}, {"question":"What is
{"quiz":
[{"question":"What is your favorite color?",
"choices":[{"prefix":"A","content":"Red"},{"prefix":"B","content":"Blue"},{"prefix":"C","content":"Yellow"},{"prefix":"D","content":"Pink"}]},
{"question":"What is the equivalent measurement of 1 feet?",
"choices":[{"prefix":"A","content":"12cm"},{"prefix":"B","content":"12px"},{"prefix":"C","content":"12mm"},{"prefix":"D","content":"12inch"}]},
{"question":"What is the combination of Green?",
"choices":[{"prefix":"A","content":"Yellow and Red"},{"prefix":"B","content":"Blue and Orange"},{"prefix":"C","content":"Yellow and Blue"},{"prefix":"D","content":"Black and Skyblue"}]}],"success":1}
我想把它转换成java脚本,就像这个
const myQuestions = [
{
question: "Who is the strongest?",
answers: {
a: "Superman",
b: "The Terminator",
c: "Waluigi, obviously"
},
correctAnswer: "c"
},
{
question: "What is the best site ever created?",
answers: {
a: "SitePoint",
b: "Simple Steps Code",
c: "Trick question; they're both the best"
},
correctAnswer: "c"
},
{
question: "Where is Waldo really?",
answers: {
a: "Antarctica",
b: "Exploring the Pacific Ocean",
c: "Sitting in a tree",
d: "Minding his own business, so stop asking"
},
correctAnswer: "d"
}
];
我怎样才能做到这一点,因为我正在制作一个测验应用程序,它将使用webviewer在移动设备中查看。非常感谢您的帮助。以下是您如何可能转换阵列的开始。请注意,您的输入中没有correctAnswer列,因此无法转换:
var输入={“测验”:
[{“问题”:“你最喜欢什么颜色?”,
“选项”:[{“前缀”:“A”,“内容”:“红色”},{“前缀”:“B”,“内容”:“蓝色”},{“前缀”:“C”,“内容”:“黄色”},{“前缀”:“D”,“内容”:“粉色”},},
{“问题”:“1英尺的等效尺寸是多少?”,
“选项”:[{“前缀”:“A”,“内容”:“12cm”},{“前缀”:“B”,“内容”:“12px”},{“前缀”:“C”,“内容”:“12mm”},{“前缀”:“D”,“内容”:“12inch”},
{“问题”:“绿色的组合是什么?”,
“选择”:[{“前缀”:“A”,“内容”:“黄色和红色”},{“前缀”:“B”,“内容”:“蓝色和橙色”},{“前缀”:“C”,“内容”:“黄色和蓝色”},{“前缀”:“D”,“内容”:“黑色和天蓝色”}],“成功”:1}
console.log(input.quick.map({question,choices})=>({
问题
答案:choices.reduce((obj,v)=>Object.assign(obj,{[v.prefix]:v.content}),{}),
正确答案:“?”,
})));
constmyquestions=JSON.parse(“{JSON}”);如何确定什么是正确答案
?先生,请您将其动态化,就像从php json_数组到java变量一样。非常感谢。在我的脚本中,我添加了您给出的代码。。。函数输出(){var input=JSON.parse(“”);…因为上面的JSON数组是在数据库中提取的。