Javascript - 访问具有重复名称的对象

时间:2015-06-15 21:04:00

标签: javascript arrays xml object

我有一个XML文件,我读到了Javascript对象。这叫做jsonXML。我在登录时可以在控制台中完整地看到对象。这是一个非常复杂的对象,有很多层和重复。

我需要访问其中一些重复的对象,但我似乎无法深入研究我需要的细节。我遇到的问题是重复的对象。

这是JS对象的结构:

  1. ProfileGroup:
    1. 文件:(我需要访问85个“文件对象”)
      1. 0 :(我认为在树中这是文件的数组索引)
        1. 配置文件:
          1. WayPt:(我需要访问的每个文件都有两个“WayPt”对象)
          2. 0 :(我认为在树中这是 WayPt 的数组索引)
            1. 距离
            2. localCoords
        2. WayPt:
          1. 1 :(我认为在树中这是 WayPt 的数组索引)
            1. 距离
            2. localCoords
  2. 我正在使用它来尝试访问对象:

    console.log(jsonXML.ProfileGroup.File.Profile.WayPt);
    

    我可以使用console.log(jsonXML.ProfileGroup.File);获取文件数组 但我无法获得单独的“文件”对象或其他任何东西。

    有关此问题的任何信息都会有所帮助。谢谢!

    更新

    对于那些想要看到它的人来说,这是一些加强的JSON:

    "xmlns": "www.geophysical.com/DZX/1.02",
    "GlobalProperties": {
        "verticalUnit": "cm",
        "horizontalUnit": "m",
        "dielectric": "1.8000000",
        "readOnly": "0",
        "unitsPerMark": "100.0000000",
        "unitsPerScan": "0.0200000"
    },
    "Macro": {
        "state": "0",
        "Process": [
            {
                "state": "0",
                "BinaryData": "0&``!```$$`!C``H``````\"`@\n"
            },
            {
                "state": "0",
                "BinaryData": "L+``!```$)`!-`C,ST[\\`````````````A$$````````````````````````@\n"
            },
            {
                "state": "0",
                "BinaryData": "ME@`!```$C@`[`P````!!``\"X00``A$(`````````````````````````````\nM````````````````````````````````@$H``,A\"``#_`P``````````````\nM````````````````````````````````````````````````````````````\n/````````````````````\n"
            },
            {
                "state": "0",
                "BinaryData": "M2``!```$0``$```````#```````````````````````@`V0```#_`P``````\n;````````````````````````````````````\n"
            },
            {
                "state": "0",
                "BinaryData": "0$``!```$\"``G`0```````\"`@\n"
            }
        ]
    },
    "ProfileGroup": {
        "scanRange": "0,88815",
        "Radan3D": {
            "localMinCoords": "-0.5000000,0.0000000,0.0000000",
            "localMaxCoords": "18.0000000,25.0000000,0.0000000",
            "globalMinCoords": "0.0000000,0.0000000,0.0000000",
            "globalMaxCoords": "0.0000000,0.0000000,0.0000000",
            "localRotationAngle": "0.0000000",
            "displayXDirProfs": "1",
            "displayYDirProfs": "1",
            "displayOtherDirProfs": "0",
            "hide": "0",
            "localGain": "0.0000000",
            "displayOrder": "0",
            "flipDataInVertDir": "0",
            "flipDataInHorizDir": "0"
        },
        "File": [
            {
                "scanRange": "0,887",
                "name": "HARMONY   __050.DZT",
                "Profile": {
                    "scanRange": "0,887",
                    "Comment": {
                        "scan": "0",
                        "description": "Data Collection Notes: "
                    },
                    "WayPt": [
                        {
                            "scan": "0",
                            "mark": "User",
                            "name": "Mark2",
                            "distance": "0.0000000",
                            "localCoords": "0.0000000,0.5000000,0.0000000"
                        },
                        {
                            "scan": "887",
                            "distance": "18",
                            "localCoords": "18,0.5000000,0.0000000"
                        }
                    ]
                }
            },
            {
                "scanRange": "888,1785",
                "name": "HARMONY   __051.DZT",
                "Profile": {
                    "scanRange": "888,1785",
                    "Comment": {
                        "scan": "888",
                        "description": "Data Collection Notes: "
                    },
                    "WayPt": [
                        {
                            "scan": "888",
                            "mark": "User",
                            "name": "Mark2",
                            "distance": "0.0000000",
                            "localCoords": "18.0000000,0.5208333,0.0000000"
                        },
                        {
                            "scan": "1785",
                            "distance": "17.9400000",
                            "localCoords": "0.0600000,0.5208333,0.0000000"
                        }
                    ]
                }
            },
            {
                "scanRange": "1786,2681",
                "name": "HARMONY   __052.DZT",
                "Profile": {
                    "scanRange": "1786,2681",
                    "Comment": {
                        "scan": "1786",
                        "description": "Data Collection Notes: "
                    },
                    "WayPt": [
                        {
                            "scan": "1786",
                            "mark": "User",
                            "name": "Mark2",
                            "distance": "0.0000000",
                            "localCoords": "-0.5000000,1.0416667,0.0000000"
                        },
                        {
                            "scan": "2681",
                            "distance": "17.9000000",
                            "localCoords": "17.4000000,1.0416667,0.0000000"
                        }
                    ]
                }
            },
            {
                "scanRange": "2682,3574",
                "name": "HARMONY   __053.DZT",
                "Profile": {
                    "scanRange": "2682,3574",
                    "Comment": {
                        "scan": "2682",
                        "description": "Data Collection Notes: "
                    },
                    "WayPt": [
                        {
                            "scan": "2682",
                            "mark": "User",
                            "name": "Mark2",
                            "distance": "0.0000000",
                            "localCoords": "18.0000000,1.5625000,0.0000000"
                        },
                        {
                            "scan": "3574",
                            "distance": "17.8400000",
                            "localCoords": "0.1600000,1.5625000,0.0000000"
                        }
                    ]
                }
            },
            {
                "scanRange": "3575,4470",
                "name": "HARMONY   __054.DZT",
                "Profile": {
                    "scanRange": "3575,4470",
                    "Comment": {
                        "scan": "3575",
                        "description": "Data Collection Notes: "
                    },
                    "WayPt": [
                        {
                            "scan": "3575",
                            "mark": "User",
                            "name": "Mark2",
                            "distance": "0.0000000",
                            "localCoords": "-0.5000000,2.0833333,0.0000000"
                        },
                        {
                            "scan": "4470",
                            "distance": "17.9000000",
                            "localCoords": "17.4000000,2.0833333,0.0000000"
                        }
                    ]
                }
            },
            {
                "scanRange": "4471,5370",
                "name": "HARMONY   __055.DZT",
                "Profile": {
                    "scanRange": "4471,5370",
                    "Comment": {
                        "scan": "4471",
                        "description": "Data Collection Notes: "
                    },
                    "WayPt": [
                        {
                            "scan": "4471",
                            "mark": "User",
                            "name": "Mark2",
                            "distance": "0.0000000",
                            "localCoords": "18.0000000,2.6041667,0.0000000"
                        },
                        {
                            "scan": "5370",
                            "distance": "17.9800000",
                            "localCoords": "0.0200000,2.6041667,0.0000000"
                        }
                    ]
                }
            }
        ]
    }
    

    }

    文件很长,所以我在文件末尾截断它。

1 个答案:

答案 0 :(得分:0)

我实际上环顾四周并想出如何做到这一点。我发现它是在一个不同的主题下,但我认为我可以帮助其他任何人解决这个具体问题。

要获得这些多个数组项,您必须输入如下索引:

console.log(jsonXML.ProfileGroup.File["0"].Profile.WayPt["0"].distance);

在迭代对象时,您可以使用:

files=[];  //array of file names
daters=[];  //final array of arrays
tempVal =[];  //temporary array
subArray=[];  //temporary array of sub values
var filz = jsonXML.ProfileGroup.File.length;
//depending on how many files there are, find out how many
for (i=0;i<filz;i++)
{
  //add each file name to an array called 'files'
  files.push(jsonXML.ProfileGroup.File[i].name);
  //since in my example there are two objects called WayPt
  for (j=0; j<2; j++)
  {
    //I use an array here to make it easy to combine into the other array
    tempVal.push(jsonXML.ProfileGroup.File[i].Profile.WayPt[j].distance);
    //since I have a string of comma separated values, make an array of these
    subArray = jsonXML.ProfileGroup.File[i].Profile.WayPt[j].localCoords.split(',');
    //combine the two above arrays
    arrayNew = tempVal.concat(subArray)
    push this array into the final array to use later
    daters.push(arrayNew);
    //reset the temporary values otherwise they will keep getting larger
    tempVal=[];
    arrayNew=[];
    subArray=[]; 
  }
}

当您在命名字段中有多个条目时,这是向下钻取对象的方式。

希望这有助于其他任何人!

相关问题