【问题标题】:How transform this json to an array like that?如何将此json转换为这样的数组?
【发布时间】:2019-09-25 09:57:55
【问题描述】:

我遇到了问题。我在 python 烧瓶中使用 mcq 生成器。我的后端工作,但我的字体结尾有问题(我以前从未使用过 javascript)。

我的 python 生成器给了我一个这样的 json:

["{\"isMulti\": false, \"question\": {\"en\": \"What is the maximum number of IP addresses that can be assigned to hosts on a local subnet that uses the 255.255.255.224 subnet mask?\"}, \"answers\": [{\"en\": \"30\"}, {\"en\": \"14\"}, {\"en\": \"16\"}, {\"en\": \"15\"}], \"other\": {}}", "{\"isMulti\": false, \"question\": {\"en\": \"Which of this is not a class of IP address?\"}, \"answers\": [{\"en\": \"Class D\"}, {\"en\": \"Class F\"}, {\"en\": \"Class C\"}, {\"en\": \"Class E\"}], \"other\": {}}"]

我想要一个这样的 javascript 数组:

  const myQuestions = [
    {
      question: en : "What is the maximum number of IP addresses that can be assigned to hosts on a local subnet that uses the 255.255.255.224 subnet mask?",
      isMulti: "false",
      answers: {
        en: "30",
        en: "14",
        en: "16",
        en: "15",
      },
    },
    {
      question: "question2",
      isMulti: "true",
      answers: {
        en: "erer",
        en: "wqrwerq",
        en: "wrwrw"
      },
    }
]

我完全不知道这样做,我需要保留“en”信息。 “en”是翻译的关键。 可以是“fr”、“it”等

我的前端生成器使用这样的 n 数组:

 const test =  [ 
{
      question: "Where i live?",
      isMulti: "True",
      answers: {
        1: "Paris",
        2: "Madrid",
        3: "Bresil",
        4: "Russia",
        5: "Johanesbrug",
        6: "Bruxelles"
      },
    }
  ];

【问题讨论】:

  • 你不能有一个对象,其中多个键共享相同的名称,像这样:{en: "30", en: "14", en: "16"}可以有一个像这样的对象数组:[{en: "30"}, {en: "14"}, {en: "16"}]

标签: javascript arrays json transform


【解决方案1】:

正如@Rounin 所指出的,你不能完全按照你的意愿去做。然而,数组中每个元素的字符串上的 JSON.parse() 非常接近。 map() 函数允许您在每个数组成员上调用它:

const input = ["{\"isMulti\": false, \"question\": {\"en\": \"What is the maximum number of IP addresses that can be assigned to hosts on a local subnet that uses the 255.255.255.224 subnet mask?\"}, \"answers\": [{\"en\": \"30\"}, {\"en\": \"14\"}, {\"en\": \"16\"}, {\"en\": \"15\"}], \"other\": {}}", "{\"isMulti\": false, \"question\": {\"en\": \"Which of this is not a class of IP address?\"}, \"answers\": [{\"en\": \"Class D\"}, {\"en\": \"Class F\"}, {\"en\": \"Class C\"}, {\"en\": \"Class E\"}], \"other\": {}}"];
const result = input.map(x => JSON.parse(x));

现在结果[0]是:

{
    "isMulti":false,
    "question":{"en":"What is the maximum number of IP addresses that can be assigned to hosts on a local subnet that uses the 255.255.255.224 subnet mask?"},
    "answers":[{"en":"30"},{"en":"14"},{"en":"16"},{"en":"15"}],"other":{}}
}

结果[1]是:

{
    "isMulti":false,
    "question":{"en":"Which of this is not a class of IP address?"},
    "answers":[{"en":"Class D"},{"en":"Class F"},{"en":"Class C"},{"en":"Class E"}],"other":{}
}

如您所见,那里有一些您不想要的对象。如果这还不够好,您可以通过向 JSON.parse 提供 reviver 参数来调整输出。这会迭代结果,并可用于查找键名并更改关联的值。 See the documentation.

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 2013-11-10
    • 2013-12-13
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 2021-07-14
    相关资源
    最近更新 更多