0

I have a JSON file that contains an array within it. I'm able to read the data fine but unsure of how I would print the array values out. Below I'm manually selecting the 3rd car. I need to show all cars. Not all persons have the same number of cars available. Some have 1 some have 3.... I keep running into null error when I try different kinds of loops.

Thanks!

try {
    ObjectMapper mapper = new ObjectMapper();
    InputStream inputStream = new FileInputStream(new File("/Users/blah/Downloads/persons.json"));
    TypeReference<List<Person>> typeReference = new TypeReference<List<Person>>() {};
    List<Person> persons = mapper.readValue(inputStream, typeReference);
    for (Person p : persons) {
        System.out.printf("name is %s city is %s cars available %s%n",
        p.getFirstName(),
        p.getAddress().getCity(),
        p.getCars()[2]);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (JsonParseException e) {
    e.printStackTrace();
} catch (JsonMappingException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

JSON Example:

[
  {
    "firstName": "Leanne Graham",
    "lastName": "Bret",
    "age": 25,
    "address": {
      "street": "123 Main St",
      "city": "Bellevue",
      "zip": "90007"
    },
    "cars": [
      "Toyota Corolla",
      "Honda Civic",
    ]
  },
  {
    "firstName": "Steve Graham",
    "lastName": "Bret",
    "age": 25,
    "address": {
      "street": "123 Main St",
      "city": "Bellevue",
      "zip": "90007"
    },
    "cars": [
      "Toyota Corolla",
      "Honda Civic",
      "Infiniti G35"
    ]
  },
  {
    "firstName": "Sally Graham",
    "lastName": "Bret",
    "age": 25,
    "address": {
      "street": "123 Main St",
      "city": "Bellevue",
      "zip": "90007"
    },
    "cars": [
      "Toyota Corolla",
      "Honda Civic",
      "Infiniti G35"
    ]
  }
]
5
  • 3
    You should implement toString in the class person Commented Jul 19, 2020 at 12:37
  • Why do you accept the latest and the less informative answer ? Do you really find it more helpfull ? Commented Jul 19, 2020 at 13:14
  • Both gave me what I need... I picked the first. Commented Jul 19, 2020 at 13:17
  • first what ? The appearing order is random and changes. As advice, always take a few seconds to choose a one for a reason ;) Commented Jul 19, 2020 at 13:18
  • @azro As I see it my answer is 2 minutes older than yours. Apart from that, it doesn't really matter, does it? Our answers are very similar and this accept wouldn't add that much to your 24,000 reputation, so I don't see the damage here. Commented Jul 19, 2020 at 15:20

2 Answers 2

1

You can use Arrays.toString(p.getCars()) for that.
BTW you should implement (override) the toString() method in your Person class. toString() is meant to give a meaningful string representation of an object. If you have done so, you can just go

for (Person p : persons) System.out.println(p);
Sign up to request clarification or add additional context in comments.

Comments

1

You should implement that in the toString of the Person class, and to print the whole array use Arrays.toString()

public class Person{        
    public String toString(){         
        return String.format("name is %s city is %s cars available %s", p.getFirstName(),
                             p.getAddress().getCity(), Arrays.toString(p.getCars()));
    }
}

And use

for (Person p : persons) {
    System.out.println(p);
}

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.