首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何用php将json数组转换成java脚本?

如何用php将json数组转换成java脚本?
EN

Stack Overflow用户
提问于 2018-10-09 03:46:01
回答 1查看 46关注 0票数 -4

我有这些数据..。

代码语言:javascript
复制
    {"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脚本...

代码语言:javascript
复制
       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在移动设备上查看。任何帮助都非常感谢..

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-09 03:57:21

下面是如何潜在地转换数组的开始。请注意,您的输入中没有correctAnswer列,因此无法进行转换:

代码语言:javascript
复制
var input = {"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}
        
console.log(input.quiz.map(({question, choices}) => ({
  question,
  answers: choices.reduce((obj, v) => Object.assign(obj, {[v.prefix]: v.content}), {}),
  correctAnswer: "?",
})));

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52709104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档