-1

i have json data in url and the data is :

[{"ADDRESS":"الأردن-عمان","StudentID":"1234","WORK_ADDRESS":""},{"ADDRESS":"الأردن-الزرقاء","StudentID":"12345","WORK_ADDRESS":""}]

i want to read it in android. *note : the data in url ,not in local file. thank you.

  • 5
    Possible duplicate of [How to parse JSON in Android](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) – gmed Mar 19 '17 at 09:47

2 Answers2

0
JSONArray array = new JSONArray(response); 

/* response :
[{"ADDRESS":"الأردن-عمان","StudentID":"1234","WORK_ADDRESS":""},{"ADDRESS":"الأردن-الزرقاء","StudentID":"12345","WORK_ADDRESS":""}]
*/

for(int i = 0 ; i < array.length();i++){
JSONObject items = array.getJSONObject(i);
String address = items.getString("ADDRESS");
String studentId = items.getString("StudentID");
String workAddress = items.getString("WORK_ADDRESS");

//then save strings in array or in Sqlite and use it later.

}
Saeed Halawani
  • 127
  • 1
  • 4
-1

you have to learn about requests in android and i recommend you to use volley library and to parse json i recommend you to use Gson there are the example first you need to create a class Data

public class Data{
private String ADDRESS;
private int StudentID;
private String WORK_ADDRESS;
 ...
}

and then add those dependency to your project

compile 'eu.the4thfloor.volley:com.android.volley:2015.05.28' 
compile 'com.google.code.gson:gson:2.8.0'

then in your activity

  String url= "you url here";
            StringRequest request=new StringRequest(url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                   Gson gson=new Gson();
                    Type listType = new TypeToken<List<Data>>() {}.getType();
            List<Data> recived=gson.fromJson(response,listType);

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i("volleyeroor",error.getMessage());
                }
            });
 Volley.newRequestQueue(this).add(request);

look now you have the List and you can use it in UI dont forgot the permission in manifest

<uses-permission android:name="android.permission.INTERNET" />

i hope this help you