-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.alibaba.fastjson2.read; | ||
|
||
import com.alibaba.fastjson2.*; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
public class DWTest { | ||
@Test | ||
public void test_parse_object_with_array_field() { | ||
String json = "{\n" + | ||
" \"stores\": [\n" + | ||
" {\n" + | ||
" \"car\": {\n" + | ||
" \"color\": {\n" + | ||
" \"name\": \"red\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
" },\n" + | ||
" {\n" + | ||
" \"car\": {\n" + | ||
" \"color\": [\n" + | ||
" {\n" + | ||
" \"name\": \"red\"\n" + | ||
" },\n" + | ||
" {\n" + | ||
" \"name\": \"green\"\n" + | ||
" }\n" + | ||
" ]\n" + | ||
" }\n" + | ||
" }\n" + | ||
" ]\n" + | ||
" }"; | ||
JSONObject chartObject = JSON.parseObject(json); | ||
JSONArray stores = chartObject.getJSONArray("stores"); | ||
|
||
for (int i = 0; i < stores.size(); i++) { | ||
JSONObject store = stores.getJSONObject(i); | ||
|
||
Car car = JSON.parseObject(store.getString("car"), Car.class); | ||
System.out.println(JSON.toJSONString(car, JSONWriter.Feature.PrettyFormat)); | ||
} | ||
} | ||
|
||
public static class Car { | ||
private Color color; | ||
|
||
public Color getColor() { | ||
return color; | ||
} | ||
|
||
public void setColor(Color color) { | ||
this.color = color; | ||
} | ||
|
||
public void setColor(List<Color> colors) { | ||
this.color = colors.get(0); | ||
} | ||
} | ||
|
||
public static class Color { | ||
private String name; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} | ||
} |