admin管理员组

文章数量:1323707

I am working with facebook JS SDK which returns user's information in JSON format. I know how to get the response like response.email which returns email address. But how to get an element from a nested array object? Example: user's education history may contain multiple arrays and each array will have an element such as "name" of "school". I want to get the element from the last array of an object.

This is a sample JSON I got:-

"education": [
{
  "school": {
    "id": "162285817180560",
    "name": "Jhenaidah** School"
  },
  "type": "H**hool",
  "year": {
    "id": "14404**5610606",
    "name": "2011"
  },
  "id": "855**14449421"
},
{
  "concentration": [
    {
      "id": "15158**968",
      "name": "Sof**ering"
    },
    {
      "id": "20179020**7859",
      "name": "Dig**ty"
    }
  ],
  "school": {
    "id": "10827**27428",
    "name": "Univer**g"
  },
  "type": "College",
  "id": "9885**826013"
},
{
  "concentration": [
    {
      "id": "108196**810",
      "name": "Science"
    }
  ],
  "school": {
    "id": "2772**996993",
    "name": "some COLLEGE NAME I WANT TO GET"
  },
  "type": "College",
  "year": {
    "id": "1388*****",
    "name": "2013"
  },
  "id": "8811215**16"
}]

Let's say I want to get "name": "some COLLEGE NAME I WANT TO GET" from the last array. How to do that with Javascript? I hope I could explain my problem. Thank you

I am working with facebook JS SDK which returns user's information in JSON format. I know how to get the response like response.email which returns email address. But how to get an element from a nested array object? Example: user's education history may contain multiple arrays and each array will have an element such as "name" of "school". I want to get the element from the last array of an object.

This is a sample JSON I got:-

"education": [
{
  "school": {
    "id": "162285817180560",
    "name": "Jhenaidah** School"
  },
  "type": "H**hool",
  "year": {
    "id": "14404**5610606",
    "name": "2011"
  },
  "id": "855**14449421"
},
{
  "concentration": [
    {
      "id": "15158**968",
      "name": "Sof**ering"
    },
    {
      "id": "20179020**7859",
      "name": "Dig**ty"
    }
  ],
  "school": {
    "id": "10827**27428",
    "name": "Univer**g"
  },
  "type": "College",
  "id": "9885**826013"
},
{
  "concentration": [
    {
      "id": "108196**810",
      "name": "Science"
    }
  ],
  "school": {
    "id": "2772**996993",
    "name": "some COLLEGE NAME I WANT TO GET"
  },
  "type": "College",
  "year": {
    "id": "1388*****",
    "name": "2013"
  },
  "id": "8811215**16"
}]

Let's say I want to get "name": "some COLLEGE NAME I WANT TO GET" from the last array. How to do that with Javascript? I hope I could explain my problem. Thank you

Share Improve this question asked Mar 11, 2017 at 22:03 JulfikarJulfikar 1,4232 gold badges20 silver badges37 bronze badges 3
  • Related: Access / process (nested) objects, arrays or JSON – Jonathan Lonowski Commented Mar 11, 2017 at 22:07
  • 1 Related: Get the last item in an array – Jonathan Lonowski Commented Mar 11, 2017 at 22:08
  • @JonathanLonowski thanks for your ment – Julfikar Commented Mar 11, 2017 at 22:12
Add a ment  | 

4 Answers 4

Reset to default 2

Here is a JsFiddle Example

var json = '{}' // your data;

// convert to javascript object:
var obj = JSON.parse(json);

// get last item in array:
var last = obj.education[obj.education.length - 1].school.name;
// result: some COLLEGE NAME I WANT TO GET

If your json above was saved to an object called json, you could access the school name "some COLLEGE NAME I WANT TO GET" with the following:

json.education[2].school.name

If you know where that element is, then you can just select it as already mentioned by calling

var obj = FACEBOOK_ACTION;
obj.education[2].school.name

If you want to select specifically the last element, then use something like this:

obj.education[ obj.education.length - 1 ].scool.name

Try this,

if (myData.hasOwnProperty('merchant_id')) {
    // do something here
}

where JSON myData is:

{
    amount: "10.00",
    email: "[email protected]",
    merchant_id: "123",
    mobile_no: "9874563210",
    order_id: "123456",
    passkey: "1234"
}

This is a simple example for your understanding. In your scenario of nested objects, loop over your JSON data and use hasOwnProperty to check if key name exists.

本文标签: How to get specific array from JSON object with JavascriptStack Overflow