服务器响应的JSON反序列化 [英] Json Deserialization of Server Response

查看:181
本文介绍了服务器响应的JSON反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展我的第一个客户端服务器的Andr​​oid应用程序。客户即,android的应用程序提供的要求,找出没有在服务器数据库中提交的客户端产品。服务器作为字符串的android应用发送以下数据。

//服务器发送以这种形式向客户端的响应。

{产品编号:1,产品名称:谷歌Nexus 5,产品描述:全新的手机,productimage:连结5.JPG,biddate:14月14下午七时30分00秒,的currentdate:14-Feb- 14上午11时57分41秒,productrate:33500} {产品编号:5,产品名称:三星Galaxy王牌,产品描述:用于手机,productimage:Ace.jpg,biddate:15月14下午七时30分00秒,的currentdate :14月14上午11点57分41秒,productrate:8500}

以上code是当服务器发送的2款产品的细节输出。我需要所有的细节如下,并显示单独作为一个列表视图。如果有N个产品作为从服务器的响应,那么就不会有列表视图n项。我知道列表视图及其编码的工作。但我不知道如何处理这个服务器响应。

 的productid [0] = 1
产品名称[0] =谷歌Nexus 5
产品描述[0] =新手机//这应该是列表中的第一项
productimagename [0] =的Nexus 5.JPG
biddate [0] = 14-FEB-14下午7点30分○○秒
的currentdate [0] = 14-FEB-14上午十一时57分41秒
productrate [0] = 33500//同样
产品ID [1] = 5 //这是要显示在列表视图中的第二个项目
产品名称[1] =三星Galaxy王牌
产品描述[1] =使用手机
productimagename [1] = Ace.jpg
biddate [1] = 15-FEB-14下午7点30分○○秒
的currentdate [1] = 14-FEB-14上午十一时57分41秒
productrate [1] = 8500

我听说,这可以通过使用JSON反序列化来实现。但我不知道该怎么做。任何一个可以请帮我..


解决方案

 类ProductInfo {
       字符串产品描述;
       字符串的productid;
       字符串产品名称;
       串productimagename;
       串biddate;
       字符串的currentdate;
       串productrate;
 }
 类ProductListInfo
 {
    ArrayList的< ProductInfo> productList的=新的ArrayList< ProductInfo>();
 }  公共无效parseResponse(对象响应)
  {
      GSonBuilder gsonBuilder =新GSonBuilder();
      GSON GSON = gsonBuilder.create();
      ProductListInfo listOfProducts =新ProductListInfo();
      listOfProducts = gson.fromJson((字符串)响应,ProductListInfo.class)  }

I'm developing my first client server android application. The client i.e, the android application gives request to find out the no of products submitted by the client in the server database. The server is sending the following data as string to the android application.

//server sending the response to client in this form.

{productid:1, productname:Google Nexus 5, productdescription:New Mobile Phone, productimage:Nexus 5.jpg, biddate:14-Feb-14 7:30:00 PM, currentdate:14-Feb-14 11:57:41 AM, productrate:33500}{productid:5, productname:Samsung Galaxy Ace, productdescription:Used Mobile Phone, productimage:Ace.jpg, biddate:15-Feb-14 7:30:00 PM, currentdate:14-Feb-14 11:57:41 AM, productrate:8500}

The above code is the output when the server sends the details of 2 products. I need to separate all the details as below and display as a list view. If there are n products as the response from the server, then there would be n items in the list view. I know the working of list view and its coding. But I don't know how to process this server response.

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

I have heard that this can be accomplished by using json deserializing. But I don't know how to do it. Can any one please help me out..

解决方案

 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)

  }

这篇关于服务器响应的JSON反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆