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对象文本吗_Javascript_Json_Object_Literals - Fatal编程技术网

需要样本javascript对象文本吗

需要样本javascript对象文本吗,javascript,json,object,literals,Javascript,Json,Object,Literals,我需要一个示例js对象文本,它可以通过以下方式访问: hist.undo[0].operation[0].x // would print '2' or whatever hist.undo[0].operation[0].y // would print '21' or whatever // [...] hist.undo[2].operation[0].x // would print '32' or whatever hist.undo[2].operation[0].y // woul

我需要一个示例js对象文本,它可以通过以下方式访问:

hist.undo[0].operation[0].x // would print '2' or whatever
hist.undo[0].operation[0].y // would print '21' or whatever
// [...]
hist.undo[2].operation[0].x // would print '32' or whatever
hist.undo[2].operation[0].y // would print '12' or whatever
谢谢

()

或者更详细地说:

var hist = {
    undo: [
        {"operation": [{"x": 100, "y":100},{"x": 100, "y":100},{"x": 100, "y":100}]},
        {"operation": [{"x": 100, "y":100},{"x": 100, "y":100},{"x": 100, "y":100}]},
        {"operation": [{"x": 100, "y":100},{"x": 100, "y":100},{"x": 100, "y":100}]},
        {"operation": [{"x": 100, "y":100},{"x": 100, "y":100},{"x": 100, "y":100}]}
    ]
}

alert(hist.undo[0].operation[0].x);

测试
历史={
“撤消”:[
{
“操作”:[{“x”:“2”,“y”:“21”},{“x”:“x2”,“y”:“y21”}]
},
{
“操作”:[{“x”:“99”,“y”:“88”}]
},
{
“操作”:[{“x”:“32”,“y”:“12”}]
}
]
};
警报(历史.撤消[0].操作[0].x);
警报(历史撤消[0]。操作[0]。y);
警报(历史撤消[2]。操作[0]。x);
警报(历史。撤消[2]。操作[0]。y);

@Toader Mihai Claudiu-不。我是JSON新手,在创建对象时遇到困难。这篇文章可能会帮助您更好地理解JavaScript对象。这不是只有在有1个撤消时才有效吗?我需要多个包含多个操作的撤消对象。所以基本上,每个hist对象都包含多个undo对象。每个撤消对象包含多个操作对象,这些操作对象本身包含多个操作。谢谢你可能会感到困惑。从索引undo[2]可以清楚地看到,其中有3个undo对象。请再看一看。符号“撤消”:[意味着“撤消”是一个数组,在那个数组中,你看到3个未命名的对象。啊,我的错。谢谢。这正是我所需要的。你可能需要考虑下面的说明。“一个字符串是一个零个或多个Unicode字符序列,用双引号包起来,使用反斜杠转义”。虽然大多数解析器在没有引号的情况下工作,但最好将它们包括在内。记住
ObjectLiteral!=SJON
应该是相同的,但不要总是重复相同的用法。我已经更新了我的答案和方法,以纳入@Richardakacyberkiwi的建议
var hist = {
    undo: [
        {"operation": [{"x": 100, "y":100},{"x": 100, "y":100},{"x": 100, "y":100}]},
        {"operation": [{"x": 100, "y":100},{"x": 100, "y":100},{"x": 100, "y":100}]},
        {"operation": [{"x": 100, "y":100},{"x": 100, "y":100},{"x": 100, "y":100}]},
        {"operation": [{"x": 100, "y":100},{"x": 100, "y":100},{"x": 100, "y":100}]}
    ]
}

alert(hist.undo[0].operation[0].x);
<html>
<head>
</head>
<body>
test
<script>
hist = {
 "undo" : [
  {
   "operation": [ {"x":"2","y":"21"}, {"x":"x2","y":"y21"} ]
  },
  {
   "operation": [ {"x":"99","y":"88"} ]
  },
  {
   "operation": [ {"x":"32","y":"12"} ]
  }
 ]
 };
alert(hist.undo[0].operation[0].x);
alert(hist.undo[0].operation[0].y);
alert(hist.undo[2].operation[0].x);
alert(hist.undo[2].operation[0].y);
</script>
</body>
</html>
hist = { "undo" : [
      {"operation":[
        {"x":2,"y":21},
        {"x":3,"y":31}
      ]},
      {"operation":[
        {"x":4,"y":41},
        {"x":5,"y":51}
      ]},
      {"operation":[
        {"x":6,"y":61},
        {"x":7,"y":71}
      ]}
     ]
    }