2014-02-14 130 views
0

我正在開發我的第一個客戶端服務器android應用程序。客戶端即android應用程序發出請求以找出客戶端在服務器數據庫中提交的產品數量。服務器將以下數據作爲字符串發送給android應用程序。Json反序列化服務器響應

//服務器以這種形式向客戶端發送響應。

{productid:1,產品名稱:Google Nexus 5,產品描述:New Mobile Phone,productimage:Nexus 5.jpg,biddate:14-Feb-14 7:30:00 PM,當前日期:14-Feb-14上午11:57:41,產品編號:33500} {productid:5,產品名稱:Samsung Galaxy Ace,產品描述:使用手機,productimage:Ace.jpg,biddate:15-Feb-14 7:30:00 PM,當前日期: 14-Feb-14 11:57:41 AM,productrate:8500}

以上代碼是服務器發送2個產品的詳細信息時的輸出。我需要分離所有細節如下,並顯示爲列表視圖。如果有n個產品作爲來自服務器的響應,則列表視圖中將有n個項目。我知道列表視圖和它的編碼的工作。但我不知道如何處理這個服務器響應。

productid[0]=1 
productname[0]=Google Nexus 5 
productdescription[0]=New Mobile Phone //this should be first item in the list 
productimagename[0]=Nexus 5.jpg 
biddate[0]=14-Feb-14 7:30:00 PM 
currentdate[0]=14-Feb-14 11:57:41 AM 
productrate[0]=33500 

//similarly 
productid[1]=5   //this is the second item to be displayed in the list view 
productname[1]=Samsung Galaxy Ace 
productdescription[1]=Used Mobile Phone 
productimagename[1]=Ace.jpg 
biddate[1]=15-Feb-14 7:30:00 PM 
currentdate[1]=14-Feb-14 11:57:41 AM 
productrate[1]=8500 

我聽說這可以通過使用json反序列化來完成。但我不知道該怎麼做。任何一個可以請幫我..

回答

1
Class ProductInfo{ 
     String productdescription; 
     String productid; 
     String productname; 
     String productimagename; 
     String biddate; 
     String currentdate; 
     String productrate; 
} 
Class ProductListInfo 
{ 
    ArrayList<ProductInfo> productList = new ArrayList<ProductInfo>(); 
} 

    public void parseResponse(Object response) 
    { 
     GSonBuilder gsonBuilder = new GSonBuilder(); 
     GSon gson = gsonBuilder.create(); 
     ProductListInfo listOfProducts = new ProductListInfo(); 
     listOfProducts = gson.fromJson((String)response, ProductListInfo.class) 

    }