首先, 您需要创建要在其中映射JSON的类。
幸运的是,有一个网站,可以为你做它在这里
其次,您可以使用Google Gson库进行轻松映射
1.添加
依赖项。
dependencies { implementation 'com.google.pre.gson:gson:2.8.6' }2.从您的对象到JSON。
MyData data =new MyData() ; //initialize the constructor Gson gson = new Gson(); String Json = gson.toJson(data ); //see firstly above above //now you have the json string do whatever.
3.从JSON到object。
String jsonString =doSthToGetJson(); //http request MyData data =new MyData() ; Gson gson = new Gson(); data= gson.fromJson(jsonString,MyData.class); //now you have Pojo do whatever
有关gson的更多信息,请参见本教程。



