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) 

    }