将 json 转换为 osc 地址和参数

convert json to osc address and arguments

我正在尝试在 javascript 中创建一个通用函数,将 json 数据结构转换为 OSC 兼容格式。 OSC 表示分配给任何类型参数的“/”分隔地址字符串。

像这样的嵌套 json:

{
  "hello":"world",
  "one":{
    "two":{
      "three":[4, 5, 6, 7]
    },
    "deux":"trois",
    "zwei":3
  }
}

会导致:

[
  {
    "addr":"/hello", 
    "args":"world"
  },
  {
    "addr":"/one/two/three", 
    "args":[4, 5, 6, 7]
  },
  {
    "addr":"/one/deux", 
    "args":"trois"
  },
  {
    "addr":"/one/zwei", 
    "args":3
  },
]

我不喜欢递归函数,但我认为这是唯一的方法,所以我想到了这个:

example = {
  "hello":"world",
  "one":{
    "two":{
      "three":[4, 5, 6, 7]
    },
    "deux":"trois",
    "zwei":3
  }
}

toOSC(example)

function toOSC(json) {
  var osc_msg = [{address:""}]
  createPath(json, osc_msg,0,"")
  for(let o of osc_msg) {
    if(o.hasOwnProperty('args')) {
      console.log(o)
    }
  }
}

function createPath(obj, osc_msg, i, addr) {
  for(let m in obj) {
    osc_msg[i]['address'] += '/' + m

    if(Array.isArray(obj[m]) || typeof obj[m] !== 'object') {
      osc_msg[i]['args'] = obj[m]
      i++
      osc_msg.push({address:""})
    } else {
      i = createPath(obj[m], osc_msg, i, osc_msg[i].address)
      i++
      osc_msg.push({address:addr})
    }
  }
  return i
}

代码失败的方式是,相同深度的两个嵌套对象中的第二个对象去掉了其地址的第一部分,我无法理解周围。

我很高兴有任何想法,还有关于将 json 转换为 OSC 兼容格式的一般方法。

我想使用转换来发送带有 node.js 包 osc-min 的消息。

如果您向下传递之前遍历的键并yield向上传递结果,那就更容易了:

     function* format(obj, previous = "") {
       for(const [key, value] of Object.entries(obj)) {
         if(typeof value !== "object" || Array.isArray(value)) {
           yield { addr: previous + "/" + key, args: value };
         } else {
           yield* format(value, previous + "/" + key);
        }
      }
    }

    // That can be used as:

     const result = [...format({ a: { b: "test", d: { e: 1 }}, c: [1, 2, 3] })];
     
     console.log(result);

这个答案是很久以后的,但我想提供一个我真正理解的答案,因为我完全不理解生成器的答案。

function processObj(obj, path="/") {
  let arr = [];
  for (let key of Object.keys(obj)) {
    const value = obj[key];
     if(typeof value !== "object" || Array.isArray(value)) {
          arr.push({
            "addr": path + key,
            "args": value
          })
     } else {
       const toAdd = processObj(value, path + key + "/");
       arr = arr.concat(toAdd);
     }
  }
  return arr;
}