Reputation:
I am trying to read simple JSON data in android without using a URL.
Here is my following JSON data. Can any one help me in reading of following data?
{"_id":"550ac135a2c513115c6cfa32","UserID":195,"LoginName":"Asdfa","EmailAddress":"[email protected]","Password":"Head","Gender":1,"DateOfBirthString":"5-21-1988","DateOfBirth":"1988-05-21T00:00:00","DOB":"21/05/1988","CreatedDate":"2015-03-19T12:29:40.313Z","Status":"P","UserAge":26,"PreffredMaxAge":99,"PreffredMinAge":21,"ProfilePhoto":null,"IsUserOnline":false,"IsUserHomo":false,"ZipCode":"","IsUserSubscribed":false,"ProfileCompletionPoints":-1.0,"LastOnlineTime":null}
Upvotes: 0
Views: 199
Reputation: 5720
have a look at this..as simple
URL myURL = new URL("your url");
URLConnection tc = myURL.openConnection();
BufferedReader in //read line using InputStreamReader with obj "tc"
String line;
while ((line = in.readLine()) != null) {
JSONArray ja = new JSONArray(line);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
listItems.add(jo.getString("text")); // add in list view
}
Upvotes: 1
Reputation: 5260
Please create a model class for User with setter gatter for all property (ids name, photo etc.). I am creating only for id.
public class AppUser{
private string id,
public void getId(){
return id;
}
public void setId(String id){
this.id = id;
}
and create a array list for AppUser type.
ArrayList<AppUser> userlist = new ArrayList<AppUser>();
Now parse data :
String serverData = "your server response in json";
JSONObject jsonObj = new JSONObject(serverdata);
String id= jsonObj .getString("_id");
String userID = phone.getString("UserID");
................//parse other attribute
AppUser user = new AppUser();
user.setId(id);
................// for other attribute
//save into arraylist
userlist.add(user);
For more detail please follow below tutorials:
http://mrbool.com/how-to-use-json-to-parse-data-into-android-application/28944
http://www.vogella.com/tutorials/AndroidJSON/article.html
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
Upvotes: 1
Reputation: 1
Make a new JSONObject and pass in the json string as a argument.
try {
json_object = new JSONObject(json_str);
} catch (JSONException) {
//thrown when it cannot parse the json
;
}
Upvotes: 0