【问题标题】:How to get a particular value from the parsed JSON data? [duplicate]如何从解析的 JSON 数据中获取特定值? [复制]
【发布时间】:2020-03-26 18:13:56
【问题描述】:

我从已解析的 JSON 文件中获取以下信息。如何从最后一个“gfs”元素(或其他任何名称)中获取 [ARRAY] 元素的值?我从来没有在 JSON 上工作过,我应该知道什么才能做到这一点?

  data: [
    {
      url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
      typ: 'base',
      pos: 0,
      gfs: null
    },
    {
      url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
      typ: 'node',
      pos: 1,
      gfs: null
    },
    {
      url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
      typ: 'node',
      pos: 2,
      gfs: null
    },
    {
      url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
      typ: 'boomerang',
      pos: 3,
      gfs: [Array]
    }
  ]

[ARRAY] 包含一些信息,我需要。

我通过解析 JSON 字符串并将其打印到控制台得到了这个。

PS。仅使用 Javascript。

【问题讨论】:

  • @PrabhjotSinghKainth 我用它来获得上面显示的结果。

标签: javascript json schema


【解决方案1】:

基本上,你得到一个对象数组,并且你想访问数组的最后一个元素,你可以通过array.length - 1获取最后一个数组位置,并且访问 gfs 值。如果您想检查 gfs 值是否为数组,则可以检查 typeof gfs

var  data= [
        {
          url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
          typ: 'base',
          pos: 0,
          gfs: null
        },
        {
          url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
          typ: 'node',
          pos: 1,
          gfs: null
        },
        {
          url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
          typ: 'node',
          pos: 2,
          gfs: null
        },
        {
          url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
          typ: 'boomerang',
          pos: 3,
          gfs: ['a','b','c']
        }
      ]
    
    // You can access like this
    console.log(data[data.length-1].gfs)

【讨论】:

    【解决方案2】:

    JSON.parse() 给出 JavaScript 对象。这里是Array对象,所以可以通过循环数组或者直接指定数组索引来访问。

    var  data = [
            {
              url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
              typ: 'base',
              pos: 0,
              gfs: null
            },
            {
              url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
              typ: 'node',
              pos: 1,
              gfs: null
            },
            {
              url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
              typ: 'node',
              pos: 2,
              gfs: null
            },
            {
              url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
              typ: 'boomerang',
              pos: 3,
              gfs: [Array]
            }
          ]
    
        data.forEach(data => {
          console.log(data.gfs); //Print the gfs value of every object in the array
        })
    

    【讨论】:

      【解决方案3】:

      通过使用访问它 console.log(obj.data[3].gfs)

      我没有考虑到数据键存储在 obj 内部的数据中(我分配的)/

      【讨论】:

        【解决方案4】:

        您可以通过index 访问数组值,因此您可以使用循环按索引访问它,也可以使用index 数字直接访问它。

        查看这些文档。这将有助于更好地理解

        Loops_and_iteration

        Array

        Property accessors

        let data =  [
            {
              url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
              typ: 'base',
              pos: 0,
              gfs: null
            },
            {
              url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
              typ: 'node',
              pos: 1,
              gfs: null
            },
            {
              url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
              typ: 'node',
              pos: 2,
              gfs: null
            },
            {
              url: 'https://playy-test.s3.ap-south-1.amazonaws.com/hidden.mp4',
              typ: 'boomerang',
              pos: 3,
              gfs: [1, 2, 4, 5]
            }
          ];
        // by looping 
        for(let i = 0; i < data.length; i++) {   // iterate on data, for(initilize, condition, increment)
        	let item = data[i];                 // access item from data by bracket notation property accessors, pass index number i
        	let gfsList = item['gfs']        // grab the gfs object from item by bracket notation property accessors
        	if(Array.isArray(gfsList)) {      // check if gfs is array or not
        		for(let j = 0; j < gfsList.length; j++) {   //gfs is array so we can iterate again on each item of it
        			let gfsItem = gfsList[j];
        			console.log(gfsItem);
        		}
        	}
        }
        // direct access 
        console.log(data[data.length - 1]['gfs'])

        【讨论】:

        • 我收到错误:TypeError: Cannot read property 'gfs' of undefined
        • @LakshyaWadhwa 点击运行代码 sn-p,我认为它正在工作
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 2019-03-30
        相关资源
        最近更新 更多