Skip to content

Commit 2016466

Browse files
Json Array to Java List
1 parent 2b87146 commit 2016466

File tree

5 files changed

+111
-18
lines changed

5 files changed

+111
-18
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
<artifactId>gson</artifactId>
118118
<version>2.10.1</version>
119119
</dependency>
120+
<dependency>
121+
<groupId>org.json</groupId>
122+
<artifactId>json</artifactId>
123+
<version>20230227</version>
124+
</dependency>
120125
<dependency>
121126
<groupId>org.apache.commons</groupId>
122127
<artifactId>commons-compress</artifactId>

src/main/java/com/howtodoinjava/DoNotCommit.java

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}

src/main/java/com/howtodoinjava/newFeatures/java21/ScopedValueTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import java.util.concurrent.StructuredTaskScope;
1+
package com.howtodoinjava.newFeatures.java21;
2+
3+
//import java.util.concurrent.StructuredTaskScope;
24

35
public class ScopedValueTest {
46

57
//public static ThreadLocal<String> CONTEXT = ThreadLocal.withInitial(() -> null);
68
//public static InheritableThreadLocal<String> CONTEXT = new InheritableThreadLocal();
79

8-
private static final ScopedValue<String> CONTEXT = ScopedValue.newInstance();
10+
//private static final ScopedValue<String> CONTEXT = ScopedValue.newInstance();
911

1012

1113

@@ -33,15 +35,15 @@ public static void main(String[] args) {
3335

3436
ScopedValueTest instance = new ScopedValueTest();
3537

36-
ScopedValue.runWhere(CONTEXT, "Test Value", () -> {
38+
/*ScopedValue.runWhere(CONTEXT, "Test Value", () -> {
3739
3840
System.out.println("In parent thread start the scoped value is: " + CONTEXT.get());
3941
instance.doSomething();
4042
System.out.println("In parent thread end the scoped value is: " + CONTEXT.get());
4143
});
4244
4345
System.out.println("Outside bounded scope isBound() is: " + CONTEXT.isBound());
44-
System.out.println("Outside bounded scope the scoped value is: " + CONTEXT.orElse(null));
46+
System.out.println("Outside bounded scope the scoped value is: " + CONTEXT.orElse(null));*/
4547

4648

4749

@@ -72,17 +74,17 @@ public static void main(String[] args) {
7274
parentThread.start();*/
7375
}
7476

75-
public void doSomething() {
77+
/*public void doSomething() {
7678
System.out.println("In doSomething() and parent scope: " + CONTEXT.get());
7779
ScopedValue.runWhere(CONTEXT, "Changed Value", () -> {
7880
System.out.println("In doSomething() and child scope: " + CONTEXT.get());
7981
doSomethingAgain();
8082
});
81-
}
83+
}*/
8284

83-
public void doSomethingAgain() {
85+
/*public void doSomethingAgain() {
8486
System.out.println("In doSomethingAgain() and child scope: " + CONTEXT.get());
85-
}
87+
}*/
8688

8789
/*static void doSomething() {
8890
System.out.println("Scoped Value in doSomething(): " + CONTEXT.get());

src/main/resources/data.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{"id":1,"name":"Alex","age":41},
3+
{"id":2,"name":"Brian","age":42},
4+
{"id":3,"name":"Charles","age":43}
5+
]

0 commit comments

Comments
 (0)