【问题标题】:Iterate A JSON Object containing list/array in android Studio在 android Studio 中迭代包含列表/数组的 JSON 对象
【发布时间】: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


    【解决方案1】:
        private void getdata(){   
        String url = "https://********.herokuapp.com/";                
        RequestQueue queue = Volley.newRequestQueue(getContext());   
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {    
            @Override    
            public void onResponse(JSONArray itemArray) {  
             List<String> allDrugs = new ArrayList<String>();
             for (int i=0; i<itemArray.length(); i++) {
             JSONObject data = itemArray.getJSONObject(i);
             String name = data.getString("Drug_Name");
             allDrugs.add(name);
            }
            }    
        }, new Response.ErrorListener() {    
            @Override    
            public void onErrorResponse(VolleyError error) {    
                Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();    
            }    
        });  
        requestQueue.add(jsonArrayRequest);  
    } 
    

    可能需要稍作修改,但您或多或少可以使用此功能。

    【讨论】:

    • 收到此错误W/System.err: org.json.JSONException: Not a primitive array: class org.json.JSONArray W/System.err: at org.json.JSONArray.&lt;init&gt;(JSONArray.java:118) 在此代码:JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener&lt;JSONArray&gt;() { @Override public void onResponse(JSONArray response) { List&lt;String&gt; allDrugs = new ArrayList&lt;String&gt;(); JSONArray itemArray= null; try { itemArray = new JSONArray(response);
    • 我已经更新了我的答案,请立即查看。它必须有效。
    • 好的,我一会儿检查一下,让你知道
    猜你喜欢
    • 2018-10-17
    • 2017-10-24
    • 2017-07-18
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    相关资源
    最近更新 更多