我试图在不使用URL的情况下在android中读取简单的JSON数据。
以下是我的以下JSON数据。任何人都可以帮我阅读以下数据吗?
{"_id":"550ac135a2c513115c6cfa32","UserID":195,"LoginName":"Asdfa","EmailAddress":"adifa1@gmail.com","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}
答案 0 :(得分:1)
请为所有属性创建一个具有setter gatter的用户的模型类(ID名称,照片等)。 我只为id创建。
public class AppUser{
private string id,
public void getId(){
return id;
}
public void setId(String id){
this.id = id;
}
并为AppUser类型创建一个数组列表。
ArrayList<AppUser> userlist = new ArrayList<AppUser>();
现在解析数据:
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);
有关详细信息,请参阅以下教程:
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/
答案 1 :(得分:1)
看看这个......简单
网址myURL =新网址(“您的网址”);
URLConnection tc = myURL.openConnection();
使用带有obj“tc”
的InputStreamReader在//读取行中的BufferedReader 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
}
答案 2 :(得分:0)
创建一个新的JSONObject并将json字符串作为参数传递。
try {
json_object = new JSONObject(json_str);
} catch (JSONException) {
//thrown when it cannot parse the json
;
}