【问题标题】:How do I run a loop through JSON code to find only specific strings in javascript?如何通过 JSON 代码运行循环以仅查找 javascript 中的特定字符串?
【发布时间】:2020-03-13 10:23:39
【问题描述】:

我正在使用 Figma API 并且正在解析一些 JSON (这不是我的整个 JSON 文件)。关于 JSON 文件的一点上下文:

  • 这些 JSON 对象都不相邻。
  • 所有 JSON 对象都属于对象,但它们按顺序出现
  • 它们是无限数量的absoluteBoundingBox
  • absoluteBoundingBox 可以使用其他名称,我不确定 名称是什么或将是什么。

我遇到的问题是:

  • 我需要遍历每个absoluteBoundingBox
  • absoluteBoundingBox 一个唯一的id
  • 和它自己的xywidthheight变量`
  • 将这些存储在一个新变量中,以便该变量包含 每次出现absoluteBoundingBox 时都需要创建对象。

我知道这是一项很大的工作。但我需要知道从哪里开始。

JSON

[{
  id: "6:3",
  absoluteBoundingBox: [{
    x: -406,
    y: -274,
    width: 437,
    height: 56
  }]
}],
[{
  id: "10:3",
  absoluteBoundingBox: [{
    x: -406,
    y: -201,
    width: 437,
    height: 56
  }]
}],
[{
  id: "10:4",
  absoluteBoundingBox: [{
    x: -406,
    y: -122,
    width: 437,
    height: 56
  }]
}],
[{
  id: "10:5",
  absoluteBoundingBox: [{
    x: -406,
    y: -28,
    width: 437,
    height: 56
  }]
}]

JS

const frameJSON = {};
const getFrame =
  args.document["children"][0]["children"][0]["absoluteBoundingBox"];

var manyFrames = args.shift().filter(function(v) {
  return args.every(function(a) {
    return a.indexOf(v) !== -1;
  });
});



var FrameID = {};
var textHeight = [];
var textWidth = [];
var textY = [];
var textX = [];
var FrameID = [];
var frameSize = getFrame.keys(getFrame).length;
frameJSON.getFrame = [];
for (b; b > frameJSON; b++) {
  if (b < getFrame.frameSize) {
    [x] = frameJSON.getFrame.push(getFrame[b].x);
    [y] = frameJSON.getFrame.push(getFrame[b].y);
    [width] = frameJSON.getFrame.push(getFrame[b].width);
    [height] = frameJSON.getFrame.push(getFrame[b].height);
    [id] = frameJSON.getFrame.push(getFrame[b].id);
  }
}

返回什么

\ 现在我在我的 FrameJSON OBJECT 中返回这些变量我想创建至少 4 个 FrameJSON 对象。

我试过了:

const getFrame =
  args.document["children"][0]["children"][0]["absoluteBoundingBox"];
  const listOfFrames = []

  //https://stackoverflow.com/questions/52672037/js-multiple-objects-in-an-array-stored-in-local-storage

  getFrame.push('absoluteBoundingBox');
  getFrame.push('id');
  showList.concat(JSON.parse(localStorage.getItem('listOfFrames')))
  localStorage.setItem("listOfFrames", JSON.stringify(listOfFrames));

为了获取帧列表,但代码没有像我预期的那样运行。

【问题讨论】:

    标签: javascript json regex loops oauth-2.0


    【解决方案1】:

    将原始 JSON 字符串中的 ( ) 更改为 [ ] 并将其解析为 4 个对象的数组。然后可以直接在浏览器的开发者控制台中解析 JSON 对象。

    var json=" ... paste your [] wrapped JSON string here";
    //edit: if you have no control over the source string, do a replacement before parsing:
    json=json.replace(/\(/g,'[').replace(/\)/g,']');
    
    var objs=eval('('+json+')');
    for (k in objs){
      var obj=objs[k];
      //do whatever you want with the obj here
    }
    

    要进一步说明格式的差异,请在浏览器控制台中尝试:

    objs=({a:'123',b:'xx'},{a:'222',b:'yy'});
    

    这将给出一个元素的对象,因为 ( ) 格式不正确。

    但是这个:

    objs=[{a:'123',b:'xx'},{a:'222',b:'yy'}];
    

    给出两个对象。然而,当一个 JSON 字符串被传输时,它是一个字符串,所以下面给出的不是正确的集合,而是一个字符串:

    json="[{a:'123',b:'xx'},{a:'222',b:'yy'}]";
    

    转换 JSON 对象的字符串表示的一个很好的技巧是像这样评估它:

    json2="([{a:'123',b:'xx'},{a:'222',b:'yy'}])";
    

    注意添加的括号!

    objs=eval(json2); //this gives you a proper array
    

    但是,您的原始字符串有 () 而不是 [],所以字符串替换是必要的。

    【讨论】:

    • 感谢您的回复。我想我知道你想说什么。您能否在答案中添加更多内容,以便我了解您要做什么。我也无法更改 JSON 文件,因为 JSON 是从 API 中提取的。 @IanHeister
    • 我已经添加了两行来解决这个问题。
    • 解析前的替换对我的 JSON 有什么作用你有例子吗? @伊恩·海斯勒
    • 在答案中添加了解释。希望这会有所帮助。
    • 我不确定您要解决什么问题?您是否只是说我无法使用 JSON,因为它的格式不正确,因为这不是上述问题。我正在使用 Figma 提供的 JSON。
    猜你喜欢
    • 2017-09-30
    • 1970-01-01
    • 2011-07-24
    • 2011-08-07
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2020-03-07
    相关资源
    最近更新 更多