I have been trying to parse json with volley , somehow i managed to extract some data but i am unable parse other fields . What i am trying do is checking which user role is doctor and store only that user to a1 arraylist
我一直试图用齐射解析json,不知怎的,我设法提取了一些数据,但我无法解析其他字段。我正在尝试做的是检查哪个用户角色是医生并且仅将该用户存储到a1 arraylist
{"user1":{"password":"******","presence":{"lastSeen":1484695229773,"status":"online"},"role":"Patient"},"user2":{"password":"******","presence":{"lastSeen":1484695229773,"status":"offline"},"role":"Doctor"}}
here is my code :
这是我的代码:
public class Users extends AppCompatActivity {
ListView usersList;
TextView noUsersText;
ArrayList<String> al = new ArrayList<>();
int totalUsers = 0;
ProgressDialog pd;
private Firebase mRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_users);
usersList = (ListView)findViewById(R.id.usersList);
noUsersText = (TextView)findViewById(R.id.noUsersText);
pd = new ProgressDialog(Users.this);
pd.setMessage("Loading...");
pd.show();
String url = "https://**********.firebaseio.com/users.json";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>(){
@Override
public void onResponse(String s) {
doOnSuccess(s);
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
System.out.println("" + volleyError);
}
});
RequestQueue rQueue = Volley.newRequestQueue(Users.this);
rQueue.add(request);
usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
UserDetails.chatWith = al.get(position);
startActivity(new Intent(Users.this, Chat.class));
}
});
}//oncreate ends here
public void doOnSuccess(String s){
try {
JSONObject obj = new JSONObject(s);
Iterator i = obj.keys();
String key = "";
while(i.hasNext()){
key = i.next().toString();
if(!key.equals(UserDetails.username)) {
al.add(key); /// here want to store only user with doctor role
//currently all users are displaying
}
totalUsers++;
}
} catch (JSONException e) {
e.printStackTrace();
}
if(totalUsers <=1){
noUsersText.setVisibility(View.VISIBLE);
usersList.setVisibility(View.GONE);
}
else{
noUsersText.setVisibility(View.GONE);
usersList.setVisibility(View.VISIBLE);
usersList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, al));
}
pd.dismiss();
}
}
i am successfully getting the Root Object "user1" and "user2" but i want to extract all other fields as well
我成功获取根对象“user1”和“user2”,但我想提取所有其他字段
kindly tell how i have to change my code for all fields.
请告诉我如何更改所有字段的代码。
EDITED
public void doOnSuccess(String s){
try {
JSONObject obj = new JSONObject(s);
Iterator i = obj.keys();
String key = "";
while(i.hasNext()){
key = i.next().toString();
JSONObject singleUser = (JSONObject) obj.get(key);
String role = singleUser.get("role").toString();
if(!key.equals(UserDetails.username)) {
if (role.equals("Doctor")) {
// doctors.add(keys);
al.add(key);
}
}
totalUsers++;
}
} catch (JSONException e) {
e.printStackTrace();
}
if(totalUsers <=1){
noUsersText.setVisibility(View.VISIBLE);
usersList.setVisibility(View.GONE);
}
else{
noUsersText.setVisibility(View.GONE);
usersList.setVisibility(View.VISIBLE);
usersList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, al));
}
pd.dismiss();
}
}
3 个解决方案
#1
0
This should give all users with doctor role.
这应该为所有用户提供医生角色。
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(
"sample.json"));
JSONObject jsonObject = (JSONObject) obj;
Set<String> keySet = jsonObject.keySet();
List<String> doctors = new ArrayList<>();
for (String keys : keySet) {
JSONObject singleUser = (JSONObject) jsonObject.get(keys);
String role = singleUser.get("role").toString();
if (role.equals("Doctor")) {
doctors.add(keys);
}
}
System.out.println("Doctor users are --" + doctors);
Output -
Doctor users are --[user2]
医生用户是 - [user2]
#2
1
You get the values from the User Object as follows
您可以从User Object获取值,如下所示
JSONObject user = obj.getJSONObject("user1");
String role = user.getString("role");
JSONObject presence = user.getJSONObject("presence");
String status = presence.getString("status");
#3
0
You have embedded JSON objects that can be easily accessed by the JSON object method: (assuming 'userList' is your root JSONObject);
您已经嵌入了可以通过JSON对象方法轻松访问的JSON对象:(假设'userList'是您的根JSONObject);
JSONObject user2 = userList.getJSONObject("user2");
These objects can then have their properties accessed via:
然后,可以通过以下方式访问这些对象:
String user2Password = user2.getString("password");
String user2Password = user2.getString(“password”);
Note the property 'presence' is also an embedded JSONObject, and so will have to be accessed with the JSONObject.getJSONObject() method.
请注意,属性“presence”也是嵌入式JSONObject,因此必须使用JSONObject.getJSONObject()方法进行访问。
So if you want to store these values in an ArrayList, simply create:
因此,如果要将这些值存储在ArrayList中,只需创建:
ArrayList<JSONObject> userArrayList = new ArrayList<JSONObject>();
... and then proceed to store values (probably iteratively) with:
...然后继续存储值(可能是迭代地):
userArrayList.add(user2);
#1
0
This should give all users with doctor role.
这应该为所有用户提供医生角色。
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(
"sample.json"));
JSONObject jsonObject = (JSONObject) obj;
Set<String> keySet = jsonObject.keySet();
List<String> doctors = new ArrayList<>();
for (String keys : keySet) {
JSONObject singleUser = (JSONObject) jsonObject.get(keys);
String role = singleUser.get("role").toString();
if (role.equals("Doctor")) {
doctors.add(keys);
}
}
System.out.println("Doctor users are --" + doctors);
Output -
Doctor users are --[user2]
医生用户是 - [user2]
#2
1
You get the values from the User Object as follows
您可以从User Object获取值,如下所示
JSONObject user = obj.getJSONObject("user1");
String role = user.getString("role");
JSONObject presence = user.getJSONObject("presence");
String status = presence.getString("status");
#3
0
You have embedded JSON objects that can be easily accessed by the JSON object method: (assuming 'userList' is your root JSONObject);
您已经嵌入了可以通过JSON对象方法轻松访问的JSON对象:(假设'userList'是您的根JSONObject);
JSONObject user2 = userList.getJSONObject("user2");
These objects can then have their properties accessed via:
然后,可以通过以下方式访问这些对象:
String user2Password = user2.getString("password");
String user2Password = user2.getString(“password”);
Note the property 'presence' is also an embedded JSONObject, and so will have to be accessed with the JSONObject.getJSONObject() method.
请注意,属性“presence”也是嵌入式JSONObject,因此必须使用JSONObject.getJSONObject()方法进行访问。
So if you want to store these values in an ArrayList, simply create:
因此,如果要将这些值存储在ArrayList中,只需创建:
ArrayList<JSONObject> userArrayList = new ArrayList<JSONObject>();
... and then proceed to store values (probably iteratively) with:
...然后继续存储值(可能是迭代地):
userArrayList.add(user2);