在Android中使用Firestore,是否可以读取下面的嵌套对象“项”中的键?
{
"email":"someemail@someaddress.com",
"items": {
"8G9RiMgx9SmDLYrQbNrN": true,
"K19RiMr7SmDLYrQbNrN": true
}
}
答案 0 :(得分:1)
可以。试试下面的代码吧:
JSONObject data = new JSONObject("youJson");
data = data.optJSONObject("items");
Iterator<String> iter = data.keys();
//Iterate all the keys
while (iter.hasNext()) {
String key = iter.next();
Boolean value = json.optBoolean(key); //Get the actual value
}
答案 1 :(得分:1)
如果您有一个表示该JSON结构的Firestore DocumentSnapshot
,则可以使用以下命令获取项的密钥:
from flask import current_app, request
from six.moves.urllib.parse import urlsplit
def endpoint_for(url, method=None, return_rule=False, follow_redirects=True):
"""
Given an absolute URL, retrieve the matching endpoint name (or rule).
Requires a current request context to determine runtime environment.
:param str method: HTTP method to use (defaults to GET)
:param bool return_rule: Return the URL rule instead of the endpoint name
:param bool follow_redirects: Follow redirects to final endpoint
:return: Endpoint name or URL rule, or `None` if not found
"""
parsed_url = urlsplit(url)
if not parsed_url.netloc:
# We require an absolute URL
return
# Take the current runtime environment...
environ = dict(request.environ)
# ...but replace the HTTP host with the URL's host...
environ['HTTP_HOST'] = parsed_url.netloc
# ...and the path with the URL's path (after discounting the app path, if not hosted at root).
environ['PATH_INFO'] = parsed_url.path[len(environ['SCRIPT_NAME']):]
# Create a new request with this environment...
url_request = current_app.request_class(environ)
# ...and a URL adapter with the new request.
url_adapter = current_app.create_url_adapter(url_request)
# Domain or subdomain must match.
# TODO: To support apps that host multiple domains, we need to remove this
# check, or offer a callback hook to check the domain.
if parsed_url.netloc != url_adapter.server_name and not (
parsed_url.netloc.endswith('.' + url_adapter.server_name)):
return
try:
endpoint_or_rule, view_args = url_adapter.match(parsed_url.path, method, return_rule=return_rule)
return endpoint_or_rule
except RequestRedirect as r:
# A redirect typically implies `/folder` -> `/folder/`
# This will not be a redirect response from a view, since the view isn't being called
if follow_redirects:
return endpoint_for(r.new_url, method=method, return_rule=return_rule, follow_redirects=follow_redirects)
except HTTPException as e:
pass
# If we got here, no endpoint was found.
答案 2 :(得分:1)
您可以这样迭代map
来实现此目的:
yourRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
Map<String, Object> items = (Map<String, Object>) document.get("items");
for (Map.Entry<String, Object> entry : items.entrySet()) {
Log.d(TAG, entry.getKey() + "/" + entry.getValue());
}
}
}
}
});