需要用到第三方jar包,添加依赖
compile ':gson:2.7'
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_main);
Gson gson = new Gson();
String s = (new ToJosn( "duan", "hubei"));
}
class ToJosn {
public String name;
public String city;
public ToJosn( String name, String city) {
= name;
= city;
}
}
这样s就是转换成json格式的数据了