This example will tell you how to use org.json.JSONObject and com.google.gson.Gson to parse JSON format string read from a URL page.
If you can not watch the above video, you can see it on the youtube URL https://youtu.be/E0hNxVfjbS4
1. Parse JSON Use JSONObject And Gson Overview.
- In this example, when you click the first button, it will use OKHttp3 to read the JSON file from the URL. And use JSONObject to parse the JSON string, then display the parsed out string in text view.
- Click the second button will do similar things to the first button, the only difference is that this button uses google Gson open-source library to parse the JSON string.
- Click the third button will clear the text view content.
- JSONObject is an android SDK built-in library, but if you want to use the Google Gson library, you need to add dependency in the project app/build.gradle file as below.
- Because this example uses OkHttp3 also, so you need to add the dependency also.
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.android.support:design:26.+' compile 'com.android.support:percent:24.2.1' compile 'com.android.support:recyclerview-v7:26.+' compile 'com.google.android:flexbox:0.3.0' compile 'com.google.code.gson:gson:2.8.2' compile 'com.squareup.okhttp3:okhttp:3.10.0' testCompile 'junit:junit:4.12' }
2. Parse JSON Example.
- Below is the example source code.
D:\WORK\DEV2QA.COM-EXAMPLE-CODE\ANDROIDEXAMPLEPROJECT\EXAMPLE\APP\SRC\MAIN\JAVA\COM\DEV2QA\EXAMPLE\JSON │ employee.json │ JSonExampleActivity.java │ └─dto UserInfoDTO.java
2.1 Main Activity.
- JSonExampleActivity.java
package com.dev2qa.example.json; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.dev2qa.example.R; import com.dev2qa.example.json.dto.UserInfoDTO; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.util.List; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class JSonExampleActivity extends AppCompatActivity { // Use JSONObject to parse json string button. private Button parseJsonUseJSONObjectButton = null; // Use GSON to parse json string button. private Button parseJsonUseGSONButton = null; // Clear parse result text view content button. private Button parseResultClearButton = null; // Display parsed json result text view. private TextView showJsonParseResultTextView = null; // Waiting for child thread message and display parsed out json text in text view. private Handler updateUIHandler = null; // Message type. private final int MESSAGE_SHOW_PARSE_RESULT = 1; // Save parsed out json string bundle key. private final String KEY_RESULT_DATA = "KEY_RESULT_DATA"; // Used to access json url page and get response data. private static OkHttpClient okHttpClient = null; // This is the json file url. private String jsonFileUrl = "http://dev2qa.com/demo/json/employee.json"; // Log debug or error message tag. private static final String TAG_JSON_EXAMPLE = "TAG_JSON_EXAMPLE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_json_example); setTitle("dev2qa.com - Android JSON Example."); // Init UI controls. initControls(); // Click this button to parse url json string with JSONObject. parseJsonUseJSONObjectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Create a callback object. Callback callback = new Callback() { // If read json file failure. @Override public void onFailure(Call call, IOException e) { // Display error message in text view. sendDisplayParseJsonResultMessage(e.getMessage()); Log.e(TAG_JSON_EXAMPLE, e.getMessage(), e); } // If read json file success. @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()) { // Get json string. String jsonString = response.body().string(); // Use JSONObject to parse json string. String parsedString = parseJSONWithJSONObject(jsonString); // Display parse result. sendDisplayParseJsonResultMessage(parsedString); } } }; // Send http get request to json page url. sendHttpGetRequest(jsonFileUrl, callback); } }); // Click this button to parse json string use GSON. parseJsonUseGSONButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Create a callback object. Callback callback = new Callback() { // If read json file url failure. @Override public void onFailure(Call call, IOException e) { sendDisplayParseJsonResultMessage(e.getMessage()); Log.e(TAG_JSON_EXAMPLE, e.getMessage(), e); } @Override public void onResponse(Call call, Response response) throws IOException { // If read json file url success. if(response.isSuccessful()) { // Get json string. String jsonString = response.body().string(); // Use GSON to parse json string. String parsedString = parseJSONWithGSON(jsonString); // Show parsed out string in text view. sendDisplayParseJsonResultMessage(parsedString); } } }; // Send http get request to json page url. sendHttpGetRequest(jsonFileUrl, callback); } }); // Click this button to clear bottom json parsed result text view content. parseResultClearButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showJsonParseResultTextView.setText(""); } }); } // Initialize UI controls. private void initControls() { if(parseJsonUseJSONObjectButton == null) { parseJsonUseJSONObjectButton = (Button)findViewById(R.id.parse_json_use_jsonobject_button); } if(parseJsonUseGSONButton == null) { parseJsonUseGSONButton = (Button)findViewById(R.id.parse_json_use_gson_button); } if(parseResultClearButton == null) { parseResultClearButton = (Button)findViewById(R.id.parse_json_clear_text_view_button); } if(showJsonParseResultTextView == null) { showJsonParseResultTextView = (TextView)findViewById(R.id.parse_json_result_text_view); } if(updateUIHandler == null) { // This handler wait for child thread message to update UI. // This can avoid update UI in child thread which can cause error. updateUIHandler = new Handler() { @Override public void handleMessage(Message msg) { // If message means show json parse result. if(msg.what == MESSAGE_SHOW_PARSE_RESULT) { // Get message saved data. Bundle bundle = msg.getData(); String resultData = bundle.getString(KEY_RESULT_DATA); // Show parsed result. showJsonParseResultTextView.setText(resultData); } } }; } if(okHttpClient == null) { okHttpClient = new OkHttpClient(); } } /* Send http get request to url. * The callback input parameter is used to receive server response data. * */ public void sendHttpGetRequest(String url, okhttp3.Callback callback) { // Create a OkHttpClient request builder. Request.Builder builder = new Request.Builder(); // Set xml file url. builder = builder.url(url); // Build http request. Request request = builder.build(); // Create a OkHttp3 Call object. Call call = okHttpClient.newCall(request); // Execute the get xml file request asynchronously in an automate child thread. call.enqueue(callback); } /* Use JSONObject to parse json string, return formatted string. */ public String parseJSONWithJSONObject(String jsonString) { StringBuffer retBuf = new StringBuffer(); try { // Create a JSONArray. JSONArray jsonArray = new JSONArray(jsonString); // Get the json array length and loop for each json object. int arrayLength = jsonArray.length(); for(int i = 0; i < arrayLength; i++) { // Get each json object. JSONObject jsonObject = (JSONObject) jsonArray.get(i); // Get each json filed value. String user = jsonObject.getString("user"); String email = jsonObject.getString("email"); String title = jsonObject.getString("title"); retBuf.append("\r\nuser = " + user); retBuf.append("\r\nemail = " + email); retBuf.append("\r\ntitle = " + title); retBuf.append("\r\n**************************"); } }catch(JSONException ex) { Log.e(TAG_JSON_EXAMPLE, ex.getMessage(), ex); retBuf.append(ex.getMessage()); }finally { return retBuf.toString(); } } /* Use GSON to parse json string, return formatted string. */ public String parseJSONWithGSON(String jsonString) { StringBuffer retBuf = new StringBuffer(); // Create a Gson object. Gson gson = new Gson(); // Create a TypeToken. Because our jason file contains multiple json object, so the TypeToken is a list of UserInfoDTO. TypeToken<List<UserInfoDTO>> typeToken = new TypeToken<List<UserInfoDTO>>(){}; // Get the type list from json string. List<UserInfoDTO> userInfoDTOList = gson.fromJson(jsonString, typeToken.getType()); // Loop the user info dto list. for(UserInfoDTO userInfoDto : userInfoDTOList) { retBuf.append("\r\nuser = " + userInfoDto.getUser()); retBuf.append("\r\nemail = " + userInfoDto.getEmail()); retBuf.append("\r\ntitle = " + userInfoDto.getTitle()); retBuf.append("\r\n**************************"); } return retBuf.toString(); } /* Send display text message to activity main thread Handler to show text in text view.*/ public void sendDisplayParseJsonResultMessage(String text) { // Create a message. Message message = new Message(); // Set message type. message.what = MESSAGE_SHOW_PARSE_RESULT; // Create a bundle and set data. Bundle bundle = new Bundle(); bundle.putString(KEY_RESULT_DATA, text); message.setData(bundle); // Send the message to main thread handler. updateUIHandler.sendMessage(message); } }
2.2 User Info DTO.
- This java class is used when using google Gson to parse JSON format strings. It requires that DTO class’s object variable’s name and type same with JSON string fields.
- For example, our JSON string is as below. So the DTO should have three string fields, and the field name is user, email, and title.
{ "user" : "kevin", "email" : "kevin@dev2qa.com", "title" : "COO" }
- UserInfoDTO.java
package com.dev2qa.example.json.dto; import java.io.Serializable; public class UserInfoDTO implements Serializable { // Map to json string field user. private String user = ""; // Map to json string field email. private String email = ""; // Map to json string field title. private String title = ""; public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
2.3 Layout XML File.
- activity_json_example.xml
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="http://dev2qa.com/demo/json/employee.json"/> <Button android:id="@+id/parse_json_use_jsonobject_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Parse JSON Use JSONObject" android:textAllCaps="false"/> <Button android:id="@+id/parse_json_use_gson_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Parse JSON Use GSON" android:textAllCaps="false"/> <Button android:id="@+id/parse_json_clear_text_view_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Clear Text View" android:textAllCaps="false"/> <TextView android:id="@+id/parse_json_result_text_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
2.4 Android Manifest Xml File.
- Need declare below permission in AndroidManifest.xml file to read JSON format string from a page URL.
- AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dev2qa.example"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".json.JSonExampleActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Reference
Hey this really works great as expected. Thanks for sharing!