最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

转换JSON到OSC地址和参数

IT培训 admin 4浏览 0评论

转换JSON到OSC地址和参数

我试图在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
}
回答如下:

如果传递下来以前经历过的键和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);

转换JSON到OSC地址和参数

我试图在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
}
回答如下:

如果传递下来以前经历过的键和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);

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论