我想将此JSON文件转换为数组:
{ “元素”:[{ “LowerTolerance”:1.4, “名称”: “ABC”, “ReferenceValue”:1.5, “UpperTolerance”:1.6, “有效”:假, “值”:1.8},{ “LowerTolerance”:20, “姓名”: “高清”, “ReferenceValue”:21.5, “UpperTolerance”:23, “有效”:真正的 “价值”:22.8},{ “LowerTolerance”:4.5, “名称”: “GHI”, “ReferenceValue”:5 “UpperTolerance”:5.5, “有效”:假, “值”:4}], “KAMERA”: “C1”}
以下是有序树形式的JSON文件图片:JSON file
我想得到像lowertolerance [],name [],referencevalue []等数组。所以当我调用数组的元素时,我得到它的值。例如: name [2] = ghi或referencevalue [0] = 1.5
我发现了这个:https://stackoverflow.com/questions/6857468/converting-a-js-object-to-an-array#=
现在我遇到的问题是,当我在对象内部有一个对象时,我不知道该怎么做。
我试过了:
var o = {"Elements": [{"LowerTolerance": 1.4, "Name": "abc", "ReferenceValue": 1.5, "UpperTolerance": 1.6}, {"LowerTolerance": 1.4, "Name": "abc", "ReferenceValue": 1.5, "UpperTolerance": 1.6}, {"LowerTolerance": 1.4, "Name": "abc", "ReferenceValue": 1.5, "UpperTolerance": 1.6}], "Kamera": "c1"};
var arr = $.map(o, function(el) { return el; })
document.getElementById("output").innerHTML = arr;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output">
</div>
(需要jQuery)
输出为:“[object Object],[object Object],[object Object],c1”。 如何在主对象中获取对象?
如果你能帮助我,你会让我很开心!
以下是我的问题的一个更简单的例子:
var myObj = [{1:1, 2:2, 3:3}, {4:4, 5:5, 6:6}];
var array = $.map(myObj, function(value, index) {
return [value];
});
document.getElementById("output").innerHTML = array;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="output"></p>
答案 0 :(得分:0)
您的JSON对象:
myJson = {"Elements":[{"LowerTolerance":1.4,"Name":"abc","ReferenceValue":1.5,"UpperTolerance":1.6,"Valid":false,"Value":1.8},{"LowerTolerance":20,"Name":"def","ReferenceValue":21.5,"UpperTolerance":23,"Valid":true,"Value":22.8},{"LowerTolerance":4.5,"Name":"ghi","ReferenceValue":5,"UpperTolerance":5.5,"Valid":false,"Value":4}],"Kamera":"c1"}
创建每个数组,例如:
var lowerToleranceArray = [];
现在,遍历您的JSON并将每个值推送到数组:
for (var i=0; i<myJson.Elements.length; i++) {
lowerToleranceArray.push(myJson.Elements[i].LowerTolerance);
}
现在,您有了lowerTolerance数组。其他数组也是一样(推入同一个循环)。
答案 1 :(得分:0)
使用 v3/playlistItems
ES6
循环:
for...of
&#13;