3
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)
1
  • 1
    This is not valid Lua code. You need {} instead of [] for arrays. Commented Feb 8, 2019 at 11:38

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.