【问题标题】:jsonpatch all elements in arrayjsonpatch 数组中的所有元素
【发布时间】:2013-04-16 23:46:21
【问题描述】:

下面的帖子让我评估了使用 jasonpatch 进行 json 到 json 转换:

JSON to JSON transformer

项目可以在这里找到:

https://github.com/bruth/jsonpatch-js

我目前正在尝试更改数组中所有元素的名称,但看不到这是怎么可能的。我目前的尝试是:

var transformations = [
{ op: 'move', from:'/hits/1/_id', path: '/hits/1/pizza'}
];

这会换掉第一个元素,但我该如何进行“*”卡片类型的操作?比如:

var transformations = [
{ op: 'move', from:'/hits/*/_id', path: '/hits/*/pizza'}
];

我可以看到可能为每个元素调用转换 N 次,但这似乎是一种 hack。

【问题讨论】:

    标签: javascript json


    【解决方案1】:

    最终使用了一种方法,我将调用应用在一个循环中:

     for(i=0;i<json.hits.length;i++) {
        var transformations = [{ op: 'move', from:'/hits/'+i+'/_id', path:'/hits/'+i+'/pizza'}];
        var result = jsonpatch.apply(json,transformations);             
     }
    

    也许 jsonpatch 可以使用通配符功能?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      • 2023-02-20
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多