【问题标题】:How do I parse json keys into json objects如何将 json 键解析为 json 对象
【发布时间】:2015-03-18 14:15:39
【问题描述】:

所以我在尝试转换诸如

之类的东西时遇到了这个问题
[0]['question']: "what is 2+2",
[0]['answers'][0]: "21",
[0]['answers'][1]: "312",
[0]['answers'][2]: "4"

像这样变成一个实际格式化的 json 对象

[
  {
    'question': 'what is 2+2',
    'answers': ["21", "312", "4"]
  }
]

但我不太确定要采取什么方法来完成这项工作。

我计划通过 javascript 解析第一个中的键值并将其解码为一个 json 对象,就像通过 python 的第二个 sn-p 中一样。

您对如何执行此操作有任何想法吗?我会接受几乎任何语言的示例,因为阅读它们背后的概念应该不会太担心。

【问题讨论】:

  • 1 - 第二个对象不是 JSON。您实际上是指 JSON 还是只是指看起来像这样的 javascript 对象?
  • 2 - 你怎么知道'question' 是一个字符串对象,而'answers' 是一个数组?
  • 我确信它会遵循这种格式,因为我将是允许输入的人。他们只能提供单字符串问题。我的意思是某种数据集,python 可以基于相同的链接地图进行解析。
  • 这样你就有了数据目标格式的模型。我建议您编写一个知道如何构建模型的函数,然后逐行输入。构建一个接受模式和输入的通用函数是可能的,但会浪费精力。如果您的格式是[0, "question", "xyz"][0, "answers", 0, "21"]] 之类的行,请考虑如何将其添加到空对象中,包括构建通过对象的各种路径。这是一个好的开始。

标签: javascript python json decode encode


【解决方案1】:

类似的东西。您需要处理输入错误。

获取数据结构并根据输入向其中添加内容的函数

function add(old, input) {
  var index = input[0];
  var section = input[1];
  if (old[index] == undefined) {
    old[index] = {}
  };
  if (section == "question") {
    old[index]['question'] = input[2];
  }

  if (section == "answers") {
    var answerIndex = input[2];
    var answerValue = input[3];

    if (old[index]["answers"] == undefined) {
      old[index]["answers"] = []
    };

    old[index]["answers"][answerIndex] = answerValue
  }

  return old;
}

一些输入:

var inputs = [[0, "question", "what"],
              [0, "answers", 0, "21"],
              [0, "answers", 1, "22"]];

var result = {};

inputs.forEach(function(input) { add(result, input) })

JSON.stringify(result)


"{"0":{"question":"what","answers":["21","22"]}}"

【讨论】:

  • 我会试一试,谢谢你帮助我:)
【解决方案2】:

我认为你应该将 json 格式化如下:

{
    "questions": [
        {
            "question": "What is 2+2",
            "possible_answers": [
                {
                    "value": 1,
                    "correct": false
                },
                {
                    "value": 4,
                    "correct": true
                },
                {
                    "value": 3,
                    "correct": false
                }
            ]
        },
        {
            "question": "What is 5+5",
            "possible_answers": [
                {
                    "value": 6,
                    "correct": false
                },
                {
                    "value": 7,
                    "correct": false
                },
                {
                    "value": 10,
                    "correct": true
                }
            ]
        }
    ]
}

为此,你可以做到:

var result = {}
result.questions = []; //the questions collection

var question = {};    //the first question object
question.question = "what is 2 + 2";      
question.possible_answers = [];

var answer1 = {};
answer1.value = 1;
answer1.correct = false;

var answer2 = {};
answer2.value = 2;
answer2.correct = true;

var answer3 = {};
answer3.value = 3;
answer3.correct = false;

question.possible_answers.push(answer1);
question.possible_answers.push(answer2);
question.possible_answers.push(answer3);

result.questions.push(question); //add the first question with its possible answer to the result.

您可以自行使用jsonlint 格式化 json,然后尝试设置您的 javascript 对象以获取您想要的 json。

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-18
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多