0

enter image description here

I'd quickly explain what's going on in the JSON data above;

I have a table called messages, with some messages having a common message_id column. I grouped the messages by message_id. the boxes in red are the message_id's which have children

Now onto the question;

is it possible to access the children of the various arrays of message_id, without actually using the message_id string?

i.e iterate over the arrays

while (i < array.length) {
    array[i]
}

if it's possible how can I do it?

Below is how I currently get the first array from the data object using the array id exactly

 val jsonObject = JSONObject(response)

                        if (!jsonObject.getBoolean("error")) {
                            //getting data array from json response object
                            val dataObject = jsonObject.getJSONObject("data")
                            Log.i("MessageFragment", "[][] data array " + dataObject)
                            val array = dataObject.getJSONArray("NzbyxhmodN")

                            var i = 0
                            while (i < array.length()) {
                                //getting wallet object from json array
                                val message = array.getJSONObject(i)

                                //adding the wallet to wallet list
                                messageList!!.add(Message(
                                        message.getInt("id"),
                                        message.getInt("sender_id"),
                                        message.getInt("receiver_id"),
                                        message.getString("subject"),
                                        message.getString("message"),
                                        message.getString("message_id"),
                                        message.getString("timestamp"),
                                        message.getBoolean("isRead")
                                ))
                                i++
                            }

I want to get the arrays without using the name i.e ("NzbyxhmodN")

2 Answers 2

1

Unfortunately, you cannot model without knowing the key value. In such cases, I use this approach. It will be useful to you.

// data -> server json response
Iterator keys = data.keys();
while(keys.hasNext()) {
    // random key
    String key = (String)keys.next();
    // and value...
    JSONArray value = data.getJSONArray(key);
}
Sign up to request clarification or add additional context in comments.

Comments

-3
{
  "data": [
    {
      "minesNext": {
        "id": "b53d83f9-cffd-43fd-bab8-c6bb78bf239e",
        "active": true,
        "payoutMultiplier": 0,
        "asountMultiplier": 1,
        "ancunt": 0,
        "payout": 0,
        "updatedAt": "Mon, 17 Jun 2824 19:57:12 GMT",
        "currency": "btc",
        "game": "mines",
        "user": {
          "id": "72ab99ed-b185-4c1d-86c0-6ed3b0be46f9",
          "name": "hagemaruindian",
          "state": {
            "mines": null,
            "minesCount": 3,
            "rounds": [
              {
                "field": 7,
                "payoutMultiplier": 1.125
              }
            ]
          }
        }
      }
    }
  ]
}

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.