Last active
July 21, 2016 08:12
-
-
Save shts/58464c537e07d89ab50fe8e8f9f3d821 to your computer and use it in GitHub Desktop.
Moshiを使ってPOJOからJsonObjectとJsonArrayを作成する ref: http://qiita.com/shts/items/83d261c7666ab0069326
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
public class User { | |
@Json(name = "name") | |
String name; | |
@Json(name = "age") | |
int age; | |
public User(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
} |
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
import com.squareup.moshi.Moshi; | |
import com.squareup.moshi.Types; | |
String json = new Moshi.Builder().build().adapter(User.class).toJson(new User("hoge", 10)); |
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
{"age":10,"name":"hoge"} |
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
import com.squareup.moshi.Moshi; | |
import com.squareup.moshi.Types; | |
import java.lang.reflect.Type; | |
List<User> users = new ArrayList<User>(); | |
users.add(new User("hoge", 10)); | |
Type listOfUsersType = Types.newParameterizedType(List.class, User.class); | |
String json = new Moshi.Builder().build().adapter(listOfUsersType).toJson(users); |
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
[{"age":10,"name":"hoge"}] |
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
new Moshi.Builder().build().adapter(Types.newParameterizedType(List.class, User.class)).toJson(Observable.just(new User("hoge", 10)).toList().toBlocking().single()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment