mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Merge branch 'eyedol-update-readme-with-jsonarray-example'
* eyedol-update-readme-with-jsonarray-example: Update doc with how to parse JSON Arrays
This commit is contained in:
26
README.md
26
README.md
@@ -213,6 +213,32 @@ JsonAdapter<Event> jsonAdapter = moshi.adapter(Event.class);
|
||||
Event event = jsonAdapter.fromJson(json);
|
||||
```
|
||||
|
||||
### Parse JSON Arrays
|
||||
|
||||
Say we have a JSON string of this structure:
|
||||
|
||||
```java
|
||||
[
|
||||
{
|
||||
"rank": "4",
|
||||
"suit": "CLUBS"
|
||||
},
|
||||
{
|
||||
"rank": "A",
|
||||
"suit": "HEARTS"
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
We can now use Moshi to parse the JSON string into a `List<Card>`.
|
||||
|
||||
```java
|
||||
String cardsJsonResponse = ...;
|
||||
Type type = Types.newParameterizedType(List.class, Card.class);
|
||||
JsonAdapter<List<Card>> adapter = moshi.adapter(type);
|
||||
List<Card> cards = adapter.fromJson(cardsJsonResponse);
|
||||
```
|
||||
|
||||
### Fails Gracefully
|
||||
|
||||
Automatic databinding almost feels like magic. But unlike the black magic that typically accompanies
|
||||
|
Reference in New Issue
Block a user