Android 转换json时出错

Android 转换json时出错,android,json,Android,Json,我需要帮助转换json 这是我的密码: try { JSONObject mainJson22 = new JSONObject(reply); JSONArray jsonArray22 = mainJson22.getJSONArray("UserListByBusinessAreaContextResult"); Log.i("mainjson234","" + jsonArray22); for (int i2 = 0; i2 < jsonAr

我需要帮助转换json

这是我的密码:

try {
    JSONObject mainJson22 = new JSONObject(reply);

    JSONArray jsonArray22 = mainJson22.getJSONArray("UserListByBusinessAreaContextResult");
    Log.i("mainjson234","" +  jsonArray22);

    for (int i2 = 0; i2 < jsonArray22.length(); i2++) {
        JSONObject objJson22 = jsonArray22.getJSONObject(i2);   
//      JSONArray innerJsonArray = jsonArray.getJSONArray(i);
//      JSONObject objJson = innerJsonArray.getJSONObject(0);

        int id22 = objJson22.getInt("UserID");
        String username22 =objJson22.getString("Username"); 
        String name = objJson22.getString("Name");

        reply =  {"UserListByBusinessAreaContextResult":"{\"tableData\":[{\"UserID\":30,\"Username\":\"Teste\",\"Name\":\"Teste\"}]}"}
UserListByBusinessAreaContextResult

不是数组。这是一个JSONObject

tableData是您的JSONArray。因此,你必须:

try { 
JSONObject mainJson22 = new JSONObject(reply);
JSONArray jsonResult = mainJson22.getJSONObject("UserListByBusinessAreaContextResult");
JSONArray jsonArray22 = jsonResult.getJSONArray("tableData");
Log.i("mainjson234","" +  jsonArray22);

for (int i2 = 0; i2 < jsonArray22.length(); i2++) {

   JSONObject objJson22 = jsonArray22.getJSONObject(i2);
...
试试{
JSONObject mainJson22=新JSONObject(回复);
JSONArray jsonResult=mainJson22.getJSONObject(“UserListByBusinessAreaContextResult”);
JSONArray jsonArray22=jsonResult.getJSONArray(“tableData”);
Log.i(“mainjson234”和“+jsonArray22”);
for(int i2=0;i2
您需要更新标题,以反映您无法将字符串转换为JSON数组。例外情况是正确的-这是一个对象,而不是数组。请进一步阅读JSON及其结构。
try { 
JSONObject mainJson22 = new JSONObject(reply);
JSONArray jsonResult = mainJson22.getJSONObject("UserListByBusinessAreaContextResult");
JSONArray jsonArray22 = jsonResult.getJSONArray("tableData");
Log.i("mainjson234","" +  jsonArray22);

for (int i2 = 0; i2 < jsonArray22.length(); i2++) {

   JSONObject objJson22 = jsonArray22.getJSONObject(i2);
...