Parse JSON in Android with Gson
To parse JSON in Android and in Java more globally, there are several third party libraries and today, we’re going to learn to use Gson library.
Gson is a Java library to convert JSON to Objects and vice-versa created by Google. Here, we’re going to use Gson only to parse JSON content.
Gson lets you to convert JSON to Objects.
Gson is a Java library to convert JSON to Objects and vice-versa created by Google. Here, we’re going to use Gson only to parse JSON content.
Gson lets you to convert JSON to Objects.
It’s a long work and we can gain some time by using a tool that uses JSON format from our sample output to generate all the POJOs associated.
Named Jsonschema2Pojo, this tool is freely available on Github. We paste the content of our output sample and we configure the generation by choosing root class, package name and some other options like using long for integers.
Each POJO represents an object in our JSON sample output.
Now, we can create a simple Java project to try to parse our JSON sample output with Gson and our generated objetcs put in com.ssaurel.gson.model package.
To parse with Gson, it’s really simple because you need only to instantiate a Gson object and then to call its fromJson method with JSON source in first parameter and root class of objects to map in second parameter.
Here is example code :
Here is link of json : http://theremix.myceleb.vn/api/user/getcity
import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; import java.util.List; import android.os.AsyncTask; import android.util.Log; import com.google.gson.GsonBuilder; public class GetListCity_Json { static GetListCity_Json singleton; List<CityVN> listCity ; CityEntity beanObject; CityVN a = new CityVN(); public GetListCity_Json() { singleton = null; } public static GetListCity_Json Singleton() { if (singleton == null) { singleton = new GetListCity_Json(); } return singleton; } public List<CityVN> getCity(int page) { try { URL URL = new URL("http://theremix.myceleb.vn/api/user/getcity"); Reader reader = API.getData(URL); beanObject = new GsonBuilder().create().fromJson(reader, CityEntity.class); listCity = beanObject.getData(); Log.d("sizearr", "size arr = "+listCity.size()); } catch (MalformedURLException e) { e.printStackTrace(); } return listCity; } }
// Main class :
package com.thanhcs.example.gsondemo; import java.util.List; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import com.google.gson.Gson; public class Main extends Activity{ public static List<CityVN> listCity; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new LoadData().execute(); } public class LoadData extends AsyncTask<Void, Integer, Void>{ @Override protected Void doInBackground(Void... params) { GetListCity_Json a = new GetListCity_Json(); Main.listCity = a.Singleton().getCity(1); return null; } @Override protected void onPostExecute(Void result) { for(int i = 0 ; i < listCity.size(); i++) { Log.d("id - ", listCity.get(i).getId()); Log.d("name - ", listCity.get(i).getName()); } super.onPostExecute(result); } } }
Download sourcode demo
No comments: