Q:

Get values outside of jsonArray for loop

I'm working on a grocery app for my project and I got this JSON Array 
[
    {   
        "msg":"success",
        "id":"1904",
        "firstname":"john",
        "lastname":"doe",
        "email":"johndoe@gmail.com",
        "mobile":"10111213",
        "appapikey ":"aba27d8447acd61152ad716696465c27"
    }
]

And I'm using Volley StringRequest
val stringRequest = StringRequest(Request. Method. GET, url, Response. Listener {

  var jsonArrayInfo = JSONArray(it)

  for(i in 0 until(jsonArrayInfo.length())) {

     var userInfo = jsonArrayInfo.getJSONObject(i)

     var fName = userInfo.getString("firstname")
     var lName = userInfo.getString("lastname")
     var eMail = userInfo.getString("email")
     var moBile = userInfo.getString("mobile")
     var apiKey = userInfo.getString("appapikey ")

     Toast.makeText(view.context, "Welcome $apiKey", Toast. LENGTH_SHORT).show()

}

var i = Intent(view.context, MainActivity::class.java)
//I want to bring the values out here
//to use them in another class with putExtra()
startActivity(i)
...
...

Is there a way I can get those items (id, firstname, lastname, email,...) outside of the for loop to to putExtra() to use them in another activity?  
Thank you!  

A:

Initialize your variables out of the loop then modify them inside the loop.  Here is your code with implementations of my answer. 
val stringRequest = StringRequest(Request. Method. GET, url, Response. Listener {

var jsonArrayInfo = JSONArray(it)
var fName = ""
var lName = ""
var eMail = ""
var moBile = ""
var apiKey = ""

for(i in 0 until(jsonArrayInfo.length())) {

    var userInfo = jsonArrayInfo.getJSONObject(i)

    fName = userInfo.getString("firstname")
    lName = userInfo.getString("lastname")
    eMail = userInfo.getString("email")
    moBile = userInfo.getString("mobile")
    apiKey = userInfo.getString("appapikey ")

    Toast.makeText(view.context, "Welcome $apiKey", Toast. LENGTH_SHORT).show()

}

