Hey guys, I've written a google apps script code to display data in JSON format. Could anyone tell me what the easiest way would be to parse this data in android studio. All the tutorials available are outdated. I need to display the JSON data in my android app. Thank you in advance!
Asked
Active
Viewed 2,276 times
1
-
Removed `android-studio` tag as this tag is used for questions/issues related to the Android Studio product. Your question is a generic Android question and has nothing to do with the Android Studio product. – David Wasser Apr 14 '22 at 10:55
-
create pojo from https://www.jsonschema2pojo.org – Kishan Mevada Apr 14 '22 at 11:03
2 Answers
1
String data = YOUR_JSON;
JSONObject jsonObject = new JSONObject(data);
JSONArray jsonArray = jsonObject.getJSONArray(0);
and then use the loop to access the object from the array.

Sachin Harne
- 175
- 2
- 14
-
-
1For Kotlin you can write like this: val data: String = YOUR_JSON val jsonObject = JSONObject(data) val jsonArray = jsonObject.getJSONArray(0) – Sachin Harne Feb 22 '23 at 07:11
0
Use Volley Library as follows:
public class MainActivity extends AppCompatActivity {
// creating variables for our textview,
private TextView courseNameTV;
private ProgressBar loadingPB;
private CardView courseCV;
// below line is the variable for url from
// where we will be querying our data.
String url = "https://jsonkeeper.com/b/63OH";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// in below line we are initializing our all views.
loadingPB = findViewById(R.id.idLoadingPB);
courseCV = findViewById(R.id.idCVCourse);
courseNameTV = findViewById(R.id.idTVCourseName);
// creating a new variable for our request queue
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
// as our data is in json object format so we are using
// json object request to make data request from our url.
// in below line we are making a json object
// request and creating a new json object request.
// inside our json object request we are calling a
// method to get the data, "url" from where we are
// calling our data,"null" as we are not passing any data.
// later on we are calling response listener method
// to get the response from our API.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// inside the on response method.
// we are hiding our progress bar.
loadingPB.setVisibility(View.GONE);
// in below line we are making our card
// view visible after we get all the data.
courseCV.setVisibility(View.VISIBLE);
try {
// now we get our response from API in json object format.
// in below line we are extracting a string with its key
// value from our json object.
// similarly we are extracting all the strings from our json object.
String courseName = response.getString("courseName");
// after extracting all the data we are
// setting that data to all our views.
courseNameTV.setText(courseName);
} catch (JSONException e) {
// if we do not extract data from json object properly.
// below line of code is use to handle json exception
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
// this is the error listener method which
// we will call if we get any error from API.
@Override
public void onErrorResponse(VolleyError error) {
// below line is use to display a toast message along with our error.
Toast.makeText(MainActivity.this, "Fail to get data..", Toast.LENGTH_SHORT).show();
}
});
// at last we are adding our json
// object request to our request
// queue to fetch all the json data.
queue.add(jsonObjectRequest);
}
}
Don't forget to add permission:
<!--permissions for INTERNET-->
<uses-permission android:name="android.permission.INTERNET"/>

Param
- 268
- 1
- 3
- 12