【发布时间】:2021-11-13 11:27:57
【问题描述】:
我有一个像这样的 JSON 对象(将从flask API 返回到android Studio),
[
{
"Drug_Name": "Lepirudin"
},
{
"Drug_Name": "Cetuximab"
},
{
"Drug_Name": "Geptanolimab"
},
{
"Drug_Name": "Narsoplimab"
},
{
"Drug_Name": "Ieramilimab"
},
{
"Drug_Name": "Vibostolimab"
},
{
"Drug_Name": "Volagidemab"
},
{
"Drug_Name": "Quavonlimab"
},
{
"Drug_Name": "AK119"
},
{
"Drug_Name": "Allocetra"
}
]
这就是在 Flask 应用程序中获取这些数据的方式
myCursor=myConnection.cursor()
#query
SQLcommand="SELECT Drug_Name FROM Drug_Description"
#executing above query
myCursor.execute(SQLcommand)
row_headers=[x[0] for x in myCursor.description] #this will extract row headers
rv = myCursor.fetchall()
json_data=[]
for result in rv:
json_data.append(dict(zip(row_headers,result)))
return jsonify (json_data)
这就是我在 android Studio 中获取 json 对象的方式
String url = "https://********.herokuapp.com/";
RequestQueue queue = Volley.newRequestQueue(getContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
// When It works
public void onResponse(JSONObject response) {
try {
//get the json object and iterate it here
}, new Response.ErrorListener() {
@Override
// If it doesnt work what to do (Errors)
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),"Try
Again",Toast.LENGTH_LONG).show();
}
});
queue.add(jsonObjectRequest);
如何遍历这些值并将每个药物名称保存在 android studio (java) 中的字符串数组中。
【问题讨论】:
标签: java android json android-studio