如何访问JSON对象的特定值?

时间:2016-12-02 13:59:23

标签: javascript arrays json object multidimensional-array

我似乎无法在console.log中找到项目名称。它在位置数据中 - >定价 - >表格 - >项目 - >名称。我打算输出“玩具熊猫”。

[
  {
    "event": "recipient_completed",
    "data": {
      "id": "msFYActMfJHqNTKH8YSvF1",
      "name": "Sample Document",
      "status": "document.draft",
      "date_created": "2014-10-06T08:42:13.836022Z",
      "date_modified": "2016-03-04T02:21:13.963750Z",
      "action_date": "2016-09-02T22:26:52.227554",
      "action_by": {
        "id": "FyXaS4SlT2FY7uLPqKD9f2",
        "email": "john@appleseed.com",
        "first_name": "John",
        "last_name": "Appleseed"
      },
      "created_by": {
        "id": "FyXaS4SlT2FY7uLPqKD9f2",
        "email": "john@appleseed.com",
        "first_name": "John",
        "last_name": "Appleseed",
        "avatar": "https://pd-live-media.s3.amazonaws.com/users/FyXaS4SlT2FY7uLPqKD9f2/avatar.jpg"
      },
      "recipients": [
        {
          "id": "FyXaS4SlT2FY7uLPqKD9f2",
          "email": "john@appleseed.com",
          "first_name": "John",
          "last_name": "Appleseed",
          "role": "signer",
          "recipient_type": "Signer",
          "has_completed": true
        }
      ],
      "sent_by": {
        "id": "FyXaS4SlT2FY7uLPqKD9f2",
        "email": "john@appleseed.com",
        "first_name": "John",
        "last_name": "Appleseed",
        "avatar": "https://pd-live-media.s3.amazonaws.com/users/FyXaS4SlT2FY7uLPqKD9f2/avatar.jpg"
      },
      "metadata": {
        "salesforce_opp_id": "123456",
        "my_favorite_pet": "Panda"
      },
      "tokens": [
        {
          "name": "Favorite Animal",
          "value": "Panda"
        }
      ],
      "fields": [
        {
          "uuid": "YcLBNUKcx45UFxAK3NjLIH",
          "name": "Textfield",
          "title": "Favorite Animal",
          "value": "Panda",
          "assigned_to": {
            "id": "FyXaS4SlT2FY7uLPqKD9f2",
            "email": "john@appleseed.com",
            "first_name": "John",
            "last_name": "Appleseed",
            "role": "Signer",
            "recipient_type": "signer",
            "has_completed": true,
            "type": "recipient"
          }
        }
      ],
      "pricing": {
        "tables": [
          {
            "id": 82307036,
            "name": "PricingTable1",
            "is_included_in_total": true,
            "summary": {
              "discount": 10,
              "tax": 0,
              "total": 60,
              "subtotal": 60
            },
            "items": [
              {
                "id": "4ElJ4FEsG4PHAVNPR5qoo9",
                "qty": 1,
                "name": "Toy Panda",
                "cost": "25",
                "price": "53",
                "description": "Buy a Panda",
                "custom_fields": {
                  "sampleField": "Sample Field"
                },
                "custom_columns": {
                  "sampleColumn": "Sample Column"
                },
                "discount": 10,
                "subtotal": 60
              }
            ],
            "total": 60
          }
        ]
      },
      "tags": [
        "test tag",
        "sales",
        "support"
      ]
    }
  }
]

我真的很感激小费。谢谢

2 个答案:

答案 0 :(得分:1)

如果将JSON对象存储到名为 obj 的变量中,则可以使用以下命令访问该值(" Toy Panda")

obj.data.pricing.tables[0].items[0].name

因为表和项是数组。

答案 1 :(得分:0)

以下是一个示例示例:

<script>
       var data = '{"name": "mkyong","age": 30,"address": {"streetAddress": "88 8nd Street","city": "New York"},"phoneNumber": [{"type": "home","number": "111 111-1111"},{"type": "fax","number": "222 222-2222"}]}';

    var json = JSON.parse(data);

    alert(json["name"]); //mkyong
    alert(json.name); //mkyong
</script>

请参阅此链接:[https://www.mkyong.com/javascript/how-to-access-json-object-in-javascript/][1]

在你的情况下它应该是这样的:

var data = // your json;
var json = JSON.parse(data);
console.log(json.pricing.tables.items[0].name;