E
E
Evgeny Petrov2019-05-26 21:03:54
Java
Evgeny Petrov, 2019-05-26 21:03:54

How to loop through an array of objects in java?

Hello.
I apologize in advance if I'm asking a too primitive question, but Google does not return results or I cannot formulate the question correctly.
How can one iterate over an array of objects in Java?
those. the structure is as follows:

[
   {
      "name":"Alex",
     "age":"40"
    },
{
      "name":"John",
     "age":"40"
    }
]

In PHP it's clear that foreach is inside foreach. And here I can not understand, swears at type errors. My tried methods:
for (Object child : m.getChilds()) {
        for (String field : child){
            System.out.println(field.name);
        }
    }

Here, m.getChilds() contains the entire array, child, respectively, objects. Of course my writing is nonsense, but I hope I correctly conveyed my idea. Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TheRonCronix, 2019-05-26
@TheRonCronix

There are a lot of examples. Please: https://duckduckgo.com/?q=load+and+traverse+json+i...

I
Israpil Akhmedov, 2019-05-27
@xISRAPILx

The trick is that the compiler will not see your fields in the object class. Create a class with the desired structure and just cast an object to it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question