|
| 1 | +package com.howtodoinjava.json; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import com.google.common.reflect.TypeToken; |
| 7 | +import com.google.gson.Gson; |
| 8 | +import java.io.IOException; |
| 9 | +import java.lang.reflect.Type; |
| 10 | +import java.net.URISyntaxException; |
| 11 | +import java.net.URL; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.nio.file.Paths; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | +import lombok.AllArgsConstructor; |
| 18 | +import lombok.Data; |
| 19 | +import lombok.NoArgsConstructor; |
| 20 | +import org.json.JSONArray; |
| 21 | +import org.json.JSONObject; |
| 22 | +import org.junit.jupiter.api.Assertions; |
| 23 | + |
| 24 | +public class JsonArrayToList { |
| 25 | + |
| 26 | + public static void main(String[] args) throws IOException, URISyntaxException { |
| 27 | + |
| 28 | + URL fileUrl = JsonArrayToList.class.getClassLoader().getResource("data.json"); |
| 29 | + Path filePath = Paths.get(fileUrl.toURI()); |
| 30 | + String jsonArray = Files.readString(filePath); |
| 31 | + |
| 32 | + System.out.println(jsonArray); |
| 33 | + |
| 34 | + usingJson(jsonArray); |
| 35 | + usingGson(jsonArray); |
| 36 | + usingJackson(jsonArray); |
| 37 | + } |
| 38 | + |
| 39 | + static void usingJson(String json) { |
| 40 | + JSONArray jsonArray = new JSONArray(json); |
| 41 | + List<Person> arrayList = new ArrayList<>(); |
| 42 | + |
| 43 | + for (int i = 0; i < jsonArray.length(); i++) { |
| 44 | + |
| 45 | + JSONObject jsonPerson = jsonArray.getJSONObject(i); |
| 46 | + |
| 47 | + long id = jsonPerson.getLong("id"); |
| 48 | + String name = jsonPerson.getString("name"); |
| 49 | + int age = jsonPerson.getInt("age"); |
| 50 | + |
| 51 | + Person person = new Person(id, name, age); |
| 52 | + arrayList.add(person); |
| 53 | + } |
| 54 | + |
| 55 | + Assertions.assertEquals(3, arrayList.size()); |
| 56 | + Assertions.assertEquals(1, arrayList.get(0).getId()); |
| 57 | + Assertions.assertEquals("Alex", arrayList.get(0).getName()); |
| 58 | + Assertions.assertEquals(41, arrayList.get(0).getAge()); |
| 59 | + } |
| 60 | + |
| 61 | + static void usingGson(String jsonArray) { |
| 62 | + Gson gson = new Gson(); |
| 63 | + Type listType = new TypeToken<List<Person>>() {}.getType(); |
| 64 | + |
| 65 | + List<Person> gsonList = gson.fromJson(jsonArray, listType); |
| 66 | + Assertions.assertEquals(3, gsonList.size()); |
| 67 | + Assertions.assertEquals(1, gsonList.get(0).getId()); |
| 68 | + Assertions.assertEquals("Alex", gsonList.get(0).getName()); |
| 69 | + Assertions.assertEquals(41, gsonList.get(0).getAge()); |
| 70 | + } |
| 71 | + |
| 72 | + static void usingJackson(String jsonArray) throws JsonProcessingException { |
| 73 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 74 | + TypeReference<List<Person>> jacksonTypeReference = new TypeReference<>() {}; |
| 75 | + |
| 76 | + List<Person> jacksonList = objectMapper.readValue(jsonArray, jacksonTypeReference); |
| 77 | + Assertions.assertEquals(3, jacksonList.size()); |
| 78 | + Assertions.assertEquals(1, jacksonList.get(0).getId()); |
| 79 | + Assertions.assertEquals("Alex", jacksonList.get(0).getName()); |
| 80 | + Assertions.assertEquals(41, jacksonList.get(0).getAge()); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +@Data |
| 85 | +@NoArgsConstructor |
| 86 | +@AllArgsConstructor |
| 87 | +class Person { |
| 88 | + long id; |
| 89 | + String name; |
| 90 | + int age; |
| 91 | +} |
0 commit comments