我一直试图用齐射解析json,不知何故我设法提取了一些数据,但我无法解析其他字段。我正在尝试做的是检查哪个用户角色是医生并且仅将该用户存储到a1 arraylist
{"user1":{"password":"******","presence":{"lastSeen":1484695229773,"status":"online"},"role":"Patient"},"user2":{"password":"******","presence":{"lastSeen":1484695229773,"status":"offline"},"role":"Doctor"}}
这是我的代码:
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();
}
}
我成功获得了根对象&#34; user1&#34;和&#34; user2&#34;但我想提取所有其他字段
请告诉我如何更改所有字段的代码。
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();
}
}
答案 0 :(得分:1)
您可以从用户对象获取值,如下所示
var world = new World();
foreach (var continent in world.continents)
{
// ...
}
答案 1 :(得分:0)
这应该为所有用户提供医生角色。
Ajv
输出 -
医生用户是 - [user2]
答案 2 :(得分:0)
您已经嵌入了可以通过JSON对象方法轻松访问的JSON对象:(假设'userList'是您的根JSONObject);
JSONObject user2 = userList.getJSONObject("user2");
然后可以通过以下方式访问这些对象:
String user2Password = user2.getString("password");
请注意,属性“presence”也是一个嵌入式JSONObject,因此必须使用JSONObject.getJSONObject()方法进行访问。
因此,如果要将这些值存储在ArrayList中,只需创建:
ArrayList<JSONObject> userArrayList = new ArrayList<JSONObject>();
...然后继续使用:
存储值(可能是迭代的) userArrayList.add(user2);