简体   繁体   中英

Accessing JSON data without key

One of the Ruby programmers is sending me data from our database in the following format as an example

[
  [
    "2017-04-16T03:00:00.000Z",
    [    
[
            180,
            4,
            [
              "FY",
              "QY",
              "GO",
              "ZV"
            ],
            [
              "OS",
              "BC",
              "CK",
              "EL"
            ],
            [
              "2017-04-16T02:26:14.000Z",
              "2017-04-16T02:58:19.000Z",
              "2017-04-16T02:59:03.000Z",
              "2017-04-16T02:59:51.000Z"
            ]
          ],

The first field is the date, the next field (180) is time in minutes from midnight, the next (4) is total count, the next field is an array of position identifiers, the next is the array of people's initials, and finally the next is an array of corresponding times when the initials were occupying the positions.

I am trying to parse this data to place it is some kind of a table. Usually, I can use JSON keys as the data field in the table and it works great, but in this array of objects, I don't have that.

How would I go about placing this type of data into an HTML table?

Thanks!

The first field is the date, the next field (180) is time in minutes from midnight

Neither of these statements are true.

The top-level JSON element is an array. The first item in that array is another array. The first item in that array is a string containing a timestamp. So if your JSON object is called json , then you can access the string containing the timestamp with json[0][0] .

The second item in that inner array is another array. The first item in this third-level array is yet another array. The first item in this fourth-level deep array is 180 , the time in minutes from midnight. So you can access that with json[0][1][0][0] .

This is much easier to visualise if you indent your code consistently:

[
    [
        "2017-04-16T03:00:00.000Z",
        [
            [
                180,

Without knowing more context, this doesn't seem like a particularly sensible way of arranging the JSON. Why so many arrays? Why supply the minutes from midnight when you already have the timestamp? Why supply a count when you can count the items yourself? I would go back to the developer that's responsible for generating this JSON and double-check that this is what they intended and that there isn't a better way of representing the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM