krish
krish

Reputation: 95

How can I access values from multidimen array in lua?

k = {
  messageCode = 200,
  result = {
    data = [
      {id=7,language="Hindi"},
      {id=8,language="Tamil"}
    ]
  }
}

How do I access language here?

I have been trying this way

print(k.result.data.language)

Upvotes: 3

Views: 79

Answers (1)

csaar
csaar

Reputation: 560

Your attempt to access the table is almost right, but your table is malformed.

k = {
  messageCode = 200,
  result = {
    data = {
      {
        id = 7,
        language = "Hindi"
      },
      {
        id = 8,
        language = "Tamil"
      }
    }
  }
}
print(k.result.data[1].language)
print(k.result.data[2].language)

k.result.data is an array (numeric lua table), so you have to iterate or access them by number.

Upvotes: 4

Related Questions