如何在android studio中从这种类型的json数组中获取价值?

Posted

技术标签:

【中文标题】如何在android studio中从这种类型的json数组中获取价值?【英文标题】:How to get value from this type of json array in android studio? 【发布时间】:2020-09-09 16:02:34 【问题描述】:
["error":"no error"]
["uid":"20","uname":"Velani Hasnain Raza","uage":"13","umo":"98658912","city":"jhguva","state":"Gffhat",
"country":"Ingja","pass":"000000gg0","image":""]
["rank":"NA","total":"6","score":"3","played":"2"]

.......我的代码.....

JsonArrayRequest 请求 = new JsonArrayRequest(WebServiceUrl, new Response.Listener() @覆盖 公共无效 onResponse(JSONArray 响应) pd1.dismiss(); MyLog.p(response.toString());

            try 

                JSONArray json=new JSONArray(response);
                JSONArray array =json.getJSONArray(0);
                JSONArray array1 =json.getJSONArray(1);
                JSONArray array2 =json.getJSONArray(2);
                String error = array.getJSONObject(0).getString("error");
                if (error.equals("no error") == false)
                    Common.ShowError(getContext(), error);
                else 
                   JSONObject current = array1.getJSONObject(0);

                    name = current.getString("uname");
                    pro_userName.setText(name);

                    city = current.getString("city");
                    state = current.getString("state");
                    pro_cityState.setText(city + "," + state);

                    played = current.getString("played");
                    totalQuiz.setText(played);

                    total_score = current.getString("total");
                    user_score = current.getString("score");
                    totalMarks.setText(user_score+"/"+total_score);

                    JSONObject current2 = array2.getJSONObject(0);
                        rank = current2.getString("rank");
                    overallRank.setText(rank);

                
             catch (JSONException e) 
                Common.ShowError(getContext(), e.getMessage());
                MyLog.p(e.getMessage()+"\n catcj block");

【问题讨论】:

【参考方案1】:

首先,你需要实现 Gson Dependencies

dependencies 
    implementation 'com.google.code.gson:gson:2.8.6'

然后将Json检索到自定义对象中

ArrayList< /*your object type*/ >listError = new ArrayList<>();

Gson gson = new Gson();
try 
    JSONArray jsonArray = new JSONArray(response);
    for (int i = 0; i < jsonArray.length(); i++) 
     JSONObject jsonObject = jsonArray.getJSONObject(i);
     switch(i) 
       case 0:
          /*your custom object*/ = gson.fromJson(jsonObject.toString(), /* your Class */.class);
          listError.add(/*your custom object*/);
       break;
       case 1:
         /*
          * your next object 
          * .... and so on 
          * create different type and use gson to parse them
          */
        break;
             
     
    catch (JSONException e) 
       e.printStackTrace();

这应该可行。确保您的模型类与 JSON 参数和数据类型具有相同的名称。它将解析 JSON 数组以键入 java 列表

【讨论】:

当我添加 gson 依赖项并构建我的项目时出现这些错误.....Execution failed for task ':app:transformClassesWithDexBuilderForDebug'.

以上是关于如何在android studio中从这种类型的json数组中获取价值?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android Studios 中从 java 类到 kotlin 类

如何在 Android Studio 中从 Firebase 中查找特定值的计数?

如何在 Android Studio 中从 Apollo Client (GraphQL) 导入生成的类

如何在android studio中从0.000000、0.000001、......格式连续增加数字

如何使用firebase在android studio中从一个用户向另一个用户发送通知[重复]

如何从列表视图中从 sqlite 检索数据单击并查看不同的活动 android studio