将字符串转换为 JSONArray 结果出错

Conversion String to JSONArray result to an error

我正在尝试将字符串转换为 JSONArray 但每次出现 JSONException,我不知道如何冻结问题。

    [
         {"color_gr":0,
          "color":0,
          "profil":"",
          "name":null,
          "role":"",
          "date_last":78554,
          "date_":0,"plus":78,
          "did":"FlaRize",
          "id":"flarize45 "
         }
         ,
         {
          "color_gr":0,
          "color":1,
          "profil":"je suis ",
          "name":"#TUEURS ",
          "role":"#PROGRAMMEURSD ",
          "date_last":45888888,
          "date_":487,
          "plus":78,
          "did":"FlaRize",
          "id":"gg "
         }
     ]

这是我要转换成 JSONArray 的字符串

java代码: - response = 谁来自服务器。

 JSONArray jsonArray = new JSONArray(response);
 JSONObject jsonObject = jsonArray.getJSONObject(count);
 ElementChat elementChat = new ElementChat(
       jsonObject.getInt("color_gr"),
       jsonObject.getInt("color"),
       jsonObject.getString("profil"),
       jsonObject.getString("name"),
       jsonObject.getString("role"),
       jsonObject.getLong("date_last"),
       jsonObject.getLong("date_"),
       jsonObject.getLong("plus"),
       jsonObject.getString("did"),
       jsonObject.getString("id")
 );

我真的不知道如何在 jsonArray 中转换这个字符串。

感谢您的帮助。

试试这个:

    JSONArray jsonArray = new JSONArray(response);
    for(int i=0;i<jsonArray .length(); i++) {
       JSONObject jsonObject = jsonArray.getJSONObject(i);
       ElementChat elementChat = new ElementChat(
           jsonObject.getInt("color_gr"),
           jsonObject.getInt("color"),
           jsonObject.getString("profil"),
           jsonObject.getString("name"),
           jsonObject.getString("role"),
           jsonObject.getLong("date_last"),
           jsonObject.getLong("date_"),
           jsonObject.getLong("plus"),
           jsonObject.getString("did"),
           jsonObject.getString("id")
     );
}

Try below

一般来说,

ArrayList<OBJECT> yourArray = new Gson().fromJson(myjsonarray.toString(), new TypeToken<List<OBJECT>>(){}.getType());

在你的情况下,请按以下步骤操作

ArrayList<ElementChat> yourArray = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<ElementChat>>(){}.getType());