簡體   English   中英

訪問json中對象中的對象

[英]Accessing an object in an object in json

我有一個看起來像這樣的 Json 文件

"timeline": [
{
"t0": [
    { "x": 0.000000, "y": 0.000000, "z": 0.000000 },
    { "x": -0.120473, "y": 4.998491, "z": 0.000000 },
    { "x": -0.323933, "y": 9.994292, "z": 0.000000 },
    { "x": -0.610325, "y": 14.986026, "z": 0.000000 },
    { "x": -0.979571, "y": 19.972315, "z": 0.000000 }, {....}

等等..

所以有一個對象“時間軸”,它由 61 個對象組成,稱為“t0”、“t1”、...“t60”,每個對象都是一個數組,其中的點由 x、y 和 z 坐標組成.

那么有人可以告訴我如何訪問 t0 對象,甚至更好地訪問其中的單獨坐標嗎?

json-File 這樣的方式通常是正確的嗎?

此致

升格

data = {"timeline": [{"t0": [{ "x": 0.000000, "y": 0.000000, "z": 0.000000 },{ "x": -0.120473, "y": 4.998491, "z": 0.000000 },{ "x": -0.323933, "y": 9.994292, "z": 0.000000 },{ "x": -0.610325, "y": 14.986026, "z": 0.000000 },{ "x": -0.979571, "y": 19.972315, "z": 0.000000 }]},{"t1": [{ "x": 0.000000, "y": 0.000000, "z": 0.000000 },{ "x": -0.120473, "y": 4.998491, "z": 0.000000 },{ "x": -0.323933, "y": 9.994292, "z": 0.000000 },{ "x": -0.610325, "y": 14.986026, "z": 0.000000 },{ "x": -0.979571, "y": 19.972315, "z": 0.000000}] }]}

變量數據包含您的示例 json 對象。

let cnt = 0;
for(let obj of data.timeline){
        var str= "t"+cnt;
        for(let p of obj[str]){
            console.log("x is :"+p.x);
            console.log("y is :"+p.y);
            console.log("z is :"+p.z);
        }
        cnt=cnt+1;
}

使用它,您可以從此類 json 對象的 json 數據中提取單獨的坐標。 如果您有不同類型的 json 數據,則代碼將被更改。

在這里簡單地寫下你的邏輯

var data = {"timeline": [{"t0": [
{ "x": 0.000000, "y": 0.000000, "z": 0.000000 },
{ "x": -0.120473, "y": 4.998491, "z": 0.000000 },
{ "x": -0.323933, "y": 9.994292, "z": 0.000000 },
{ "x": -0.610325, "y": 14.986026, "z": 0.000000 },
{ "x": -0.979571, "y": 19.972315, "z": 0.000000 }
]}]}

console.log(data.timeline[0]["t0"]);

或者你可以試試這個

$.each(data.timeline, function(key, val) {console.log(val)})

代碼中的注釋顯示了每件事是什么。 不需要單獨的計數器。

 let obj = { "timeline": [{ "t0": [{ "x": 0.000000, "y": 0.000000, "z": 0.000000 }, { "x": -0.120473, "y": 4.998491, "z": 0.000000 }, { "x": -0.323933, "y": 9.994292, "z": 0.000000 }, { "x": -0.610325, "y": 14.986026, "z": 0.000000 } ] }, { "t1": [{ "x": 0.000000, "y": 0.000000, "z": 0.000000 }, { "x": -0.120473, "y": 4.998491, "z": 0.000000 }, { "x": -0.323933, "y": 9.994292, "z": 0.000000 }, { "x": -0.610325, "y": 14.986026, "z": 0.000000 } ] }, { "t2": [{ "x": 0.000000, "y": 0.000000, "z": 0.000000 }, { "x": -0.120473, "y": 4.998491, "z": 0.000000 }, { "x": -0.323933, "y": 9.994292, "z": 0.000000 }, { "x": -0.610325, "y": 14.986026, "z": 0.000000 } ] } ] }; //two different ways to access let onepoint = obj["timeline"][1]["t1"][2]["x"] console.log(onepoint); let onepointDirect = obj.timeline[1].t1[2].x console.log(onepointDirect); // timeline array process for (let i = 0; i < obj.timeline.length; i++) { // objects inside each array for (let tobj in obj.timeline[i]) { console.log("tobj", tobj); //t0,t1 etc. // array of objects (the x,y,z things for (let t = 0; t < obj.timeline[i][tobj].length; t++) { console.log(tobj, t, obj.timeline[i][tobj][t]); // each x console.log("x:", obj.timeline[i][tobj][t].x); } } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM