2017-02-21 142 views
-2

我嘗試響應嵌套的JSON數據解析,但我不能。下面的代碼是我嵌套的json。在android中嵌套的Json解析

{ 
"Result": [ 
{ 
    "Stock": { 
    "ID": 1, 
    "Code": "sample string 2", 
    "SerialNo": "sample string 3", 
    "RFID": "sample string 4", 
    "PairCode": "sample string 5", 
    "Barcode": "sample string 6", 
    "Name": "sample string 7", 
    "Description": "sample string 8", 
    "Model": "sample string 9", 
    "CategoryId": 1, 
    "BrandId": 1, 
    "ClassificationId": 1 
    }, 
    "Division": { 
    "ID": 1, 
    "Name": "sample string 2", 
    "ParentID": 1, 
    "Path": "sample string 3" 
    }, 
    "User": { 
    "ID": 1, 
    "Name": "sample string 2", 
    "Username": "sample string 3", 
    "Password": "sample string 4" 
    } 
}, 
{ 
    "Stock": { 
    "ID": 1, 
    "Code": "sample string 2", 
    "SerialNo": "sample string 3", 
    "RFID": "sample string 4", 
    "PairCode": "sample string 5", 
    "Barcode": "sample string 6", 
    "Name": "sample string 7", 
    "Description": "sample string 8", 
    "Model": "sample string 9", 
    "CategoryId": 1, 
    "BrandId": 1, 
    "ClassificationId": 1 
    }, 
    "Division": { 
    "ID": 1, 
    "Name": "sample string 2", 
    "ParentID": 1, 
    "Path": "sample string 3" 
    }, 
    "User": { 
    "ID": 1, 
    "Name": "sample string 2", 
    "Username": "sample string 3", 
    "Password": "sample string 4" 
    } 
}, 
{ 
    "Stock": { 
    "ID": 1, 
    "Code": "sample string 2", 
    "SerialNo": "sample string 3", 
    "RFID": "sample string 4", 
    "PairCode": "sample string 5", 
    "Barcode": "sample string 6", 
    "Name": "sample string 7", 
    "Description": "sample string 8", 
    "Model": "sample string 9", 
    "CategoryId": 1, 
    "BrandId": 1, 
    "ClassificationId": 1 
    }, 
    "Division": { 
    "ID": 1, 
    "Name": "sample string 2", 
    "ParentID": 1, 
    "Path": "sample string 3" 
    }, 
    "User": { 
    "ID": 1, 
    "Name": "sample string 2", 
    "Username": "sample string 3", 
    "Password": "sample string 4" 
    } 
} 
], 
"RecordCount": 1 
} 

而且這是我的響應等級

public class ResultResponseModel<T>{ 

public int RecordCount; 

public T Result[]; 

public int getRecordCount() { 
    return RecordCount; 
} 

public void setRecordCount(int recordCount) { 
    RecordCount = recordCount; 
} 

public T[] getResult() { 
    return Result; 
} 

public void setResult(T[] result) { 
    Result = result; 
} 

}

其他類

public class ServiceStockDivisionUserModel { 

public ServiceStockDivisionUserModel() 
{ 
    Stock = new ServiceStockModel(); 

    Division = new ServiceDivisionModel(); 

    User = new ServiceUserModel(); 
} 

public ServiceStockModel Stock ; 

public ServiceDivisionModel Division ; 

public ServiceUserModel User ; 


public ServiceStockModel getStock() { 
    return Stock; 
} 

public void setStock(ServiceStockModel stock) { 
    Stock = stock; 
} 

public ServiceDivisionModel getDivision() { 
    return Division; 
} 

public void setDivision(ServiceDivisionModel division) { 
    Division = division; 
} 

public ServiceUserModel getUser() { 
    return User; 
} 

public void setUser(ServiceUserModel user) { 
    User = user; 
} 

}

我嘗試播服務後,投我JSON ResultResponseModel tockDivisionUserMopdel來獲取對象。但我無法施展,任何人都可以幫助我。謝謝。

我的理由:

JSONObject jsonObj = new JSONObject(responseModel.responseString); 

Gson gson = new Gson(); 

listResultResponseModel = gson.fromJson(String.valueOf(jsonObj), ResultResponseModel.class); 

enter image description here

+0

可能重複的[如何在Android中解析JSON](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) –

+0

哦,當然不是 – artemitSoft

+0

你能告訴我原因呢? –

回答

0

在這裏,您解析的實際響應您的服務器或其他資源獲得

JSONArray result = response.optJSONArray("Result"); 
     int length = result.length(); 
     for(int i=0; i<length; i++){ 
      JSONObject userData = result.optJSONObject(i); 
      User user = new User(userData.optJSONObject("User")); 
      Division division = new Division(userData.optJSONObject("Division")); 
      Stock stock = new Stock(userData.optJSONObject("Stock")); 
     } 

User.java

public class User{ 

     private int ID; 
     private String Code; 
     //and other variables for UserData 


     public User(JSONObject userData) { 
      this.ID = userData.optInt("ID"); 
      this.Code = userData.optString("Code"); 
      //similar initializations for other variables 
     } 

     public int getID() { 
      return ID; 
     } 

     public String getCode() { 
      return Code; 
     } 
    } 

您需要爲股票和分部創建類並在內部處理JSON的類似方法。

+0

Thans尋求幫助 – artemitSoft

0
FullResponse model = new Gson().fromJson(JSON_STRING, FullResponse.class); 

作出這樣的模式。

public class FullResponse{ 
    List<ResultResponse> Result; 
    int RecordCount; 

    //setter and getter 
} 

public class ResultResponse{ 
    StockResponse Stock; 
    DivisionResponse Division; 
    UserResponse User; 

    //setter and getter 
} 

public class StockResponse{ 
    int ID; 
    String Code; 
    .... 

    //getter 
} 

public class DivisionResponse{ 
    int ID; 
    String Name; 
    .... 

    //getter 
} 

public class UserResponse{ 
    int ID; 
    String Name; 
    .... 

    //getter 
} 

可選

如果你想檢查是否您的JSON如果有效或不..

public boolean isJSONValid(String test) { 
    try { 
     new JSONObject(test); 
    } catch (JSONException ex) { 
     try { 
      new JSONArray(test); 
     } catch (JSONException ex1) { 
      return false; 
     } 
    } 
    return true; 
}