Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 重写JSON对象的结构_Javascript_Json_Javascript Objects - Fatal编程技术网

Javascript 重写JSON对象的结构

Javascript 重写JSON对象的结构,javascript,json,javascript-objects,Javascript,Json,Javascript Objects,我需要为一个项目制作很多JSON文件。这个JSON来自Google电子表格。使用我得到的JSON如下所示: { "custom_id": 1, "another_thing": "pizza", "step_1_message": "msg", "step_1_hint": "hint", "step_1_intent": "intent", "step_2_message": "msg", "step_2_hint": "hint", "step_2_inten

我需要为一个项目制作很多JSON文件。这个JSON来自Google电子表格。使用我得到的JSON如下所示:

{
  "custom_id": 1,
  "another_thing": "pizza",
  "step_1_message": "msg",
  "step_1_hint": "hint",
  "step_1_intent": "intent",
  "step_2_message": "msg",
  "step_2_hint": "hint",
  "step_2_intent": "intent"
}
现在我希望所有的步骤都是从一个对象本身开始的。像这样:

{
  "custom_id": 1,
  "another_thing": "pizza",
  "steps": [
   {"step_id": 1, "message": "msg", hint: "hint", "intent": "intent"},
   {"step_id": 2, "message": "msg", hint: "hint", "intent": "intent"}
  ]
}

以下是工作解决方案:

var input = {
  "custom_id": 1,
  "another_thing": "pizza",
  "step_1_message": "msg",
  "step_1_hint": "hint",
  "step_1_intent": "intent",
  "step_2_message": "msg",
  "step_2_hint": "hint",
  "step_2_intent": "intent"
};
var output = {
    steps: []
};
for (var key in input) {
    var m = key.match(/step_([0-9]+)_(\w+)/);
    if (m) {
        var num = m[1];
        var name = m[2];
        if (!output.steps[num-1]) {
            output.steps[num-1] = {
                step_id: num
            };
        }
        output.steps[num-1][name] = input[key];
    } else {
        output[key] = input[key];
    }
}
佩里什

尝试将每个键值对与正则表达式进行匹配,以查找与“step”匹配的键,然后将此键值对与forEach进一步匹配以获取消息、提示等。将这些值用作要创建的对象的构造函数值。
如果JSON具有不同数量的键值对,那么在对象类定义中需要一个动态构造函数

如果(!output.steps[num-1]){output.steps[num-1]={};}应该是
if(!output.steps[num-1]){output.steps[num-1]={};output.steps[num-1].step\u-id=num;
@JoelHernandez所以建议他在询问之前尝试一下,否则他就不会提高编程技能。@DontVoteMeDown我试了两天,但主要是使用.match或indexOf,这会破坏步骤的迭代。@JoelHernandez如果我不先自己尝试,我不会要求。你被否决了,因为你基本上是在要求别人为你做繁重的工作。公认的解决方案只是一个用于。。。在中,通过一些基本的regexp,您可以在JS入门课程中学习到这两个概念。所以,当你因为不确定该采取什么步骤而陷入困境时,就应该这样做,而不是因为你想要自由劳动。@RamonGebben好吧,那么为什么不把你的努力与问题联系起来呢?