如何从Android中的数组数组中获取Json数据

2020年11月17日 49点热度 0条评论

例如我有JSON格式的数组的数组:

[
  [17,"Tracker-9210671","+918010804433",true,84,"869867039210671"],
  [18,"Gtrack_Ciaz","+918010804433",true,85,"869867039215308"], 
  [20,"Gtrack_Zeep_Trunk","+918010804433",true,86,"869867039210333"], 
  [19,"Gtrack_Zeep_Dashboard","+918010804433",true,87,"869867039209095"]
]

我想在我的android中获取此Json数据,如何获取此。我无法获取此JsonArray谁能告诉我该代码是什么。

我所做的是(使用排球):

'''

 public class MainActivity extends AppCompatActivity {

 private TextView mTextViewResult;
 private RequestQueue mQueue;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  mTextViewResult = findViewById(R.id.text_view_result);
  Button buttonParse = findViewById(R.id.button_parse);

  mQueue = Volley.newRequestQueue(this);

  buttonParse.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    jsonParse();
   }
  });
 }
 private void jsonParse() {
  String url = "url";
  JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
   new Response.Listener < JSONArray > () {
    @Override
    public void onResponse(JSONArray response) { // Called when request successfull.


     try {
      for (int i = 0; i < response.length(); i++) {

       JSONArray jsonArray = response.getJSONArray(i);
       for (int j = 0; j < jsonArray.length(); j++) {
        JSONArray value = jsonArray.getJSONArray(j);

        int DB_ID = value.getInt(0);
        String Devie_Name = value.getString(1);
        String Mobile_Number = value.getString(2);
        Boolean Status = value.getBoolean(3);
        int Stock_Id = value.getInt(4);
        String Device_Id = value.getString(5);
        mTextViewResult.append(String.valueOf(DB_ID) + " , " + Devie_Name + " , " + Mobile_Number + " , " + String.valueOf(Status) + " , " + String.valueOf(Stock_Id) + " , " + Device_Id + "\n\n");

       }
      }

     } catch (JSONException e) {
      e.printStackTrace();
     }

    }


   }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) { // called when request not success.
     error.printStackTrace();
    }
   });

  mQueue.add(request);
 }
}

'''

我得到的错误:

org.json.JSONException:类型为java.lang.Integer的0处的值17不能为

转换为JSONArray。

解决方案如下:

只需将其添加到您的Android Manifest文件。因为从Android 9 OS开始,我们需要添加此属性android:usesCleartextTraffic="true"

因为从Android 9 (API level 28)开始,所以默认情况下禁用明文支持。

 <application
        android:usesCleartextTraffic="true">