如何使用gson(Java)使用JSON中的数据创建多个对象?

时间:2016-05-12 00:53:23

标签: java json gson

所以我有一些JSON我想解析成一个对象的数组列表(a)。 a将保存href链接以及文本,价格和图像链接。

这是JSON:

{  
   "url":"http://www.ebay.com/sch/i.html?_sacat=0&LH_ItemCondition=3000&_nkw=macbook%20pro%2015%202012&rt=nc&LH_BIN=1&_trksid=p2045573.m1684",
   "result":{  
      "extractorData":{  
         "url":"http://www.ebay.com/sch/i.html?_sacat=0&LH_ItemCondition=3000&_nkw=macbook%20pro%2015%202012&rt=nc&LH_BIN=1&_trksid=p2045573.m1684",
         "resourceId":"73c12187d3f6d12410d3e7490cb91d9a",
         "data":[  
            {  
               "group":[                      
                  {  
                     "Img image":[  
                        {  
                           "src":"http://thumbs.ebaystatic.com/images/g/xMYAAOSwE7RXF7p5/s-l225.jpg",
                           "href":"http://www.ebay.com/itm/15-Apple-MacBook-Pro-RETINA-2-3GHz-Core-i7-8GB-256GB-Flash-MC975LL-A-Warranty-/121983722648?hash=item1c66cbe498:g:xMYAAOSwE7RXF7p5"
                        }
                     ],
                     "Vip link":[  
                        {  
                           "text":"New listing 15\" Apple MacBook Pro RETINA -2.3GHz Core i7-8GB-256GB Flash-MC975LL/A+Warranty",
                           "href":"http://www.ebay.com/itm/15-Apple-MacBook-Pro-RETINA-2-3GHz-Core-i7-8GB-256GB-Flash-MC975LL-A-Warranty-/121983722648?hash=item1c66cbe498:g:xMYAAOSwE7RXF7p5"
                        }
                     ],
                     "Bold price":[  
                        {  
                           "text":"$989.00"
                        },
                        {  
                           "text":"$989.00"
                        }
                     ],
                     "Lvformat value":[  
                        {  
                           "text":"Buy It Now"
                        }
                     ]
                  },
                  {  
                     "Img image":[  
                        {  
                           "src":"http://ir.ebaystatic.com/pictures/aw/pics/s_1x2.gif",
                           "href":"http://www.ebay.com/itm/MINT-15-Apple-MacBook-Pro-Retina-2-3ghz-i7-8gb-256gb-w-charger-Box-/262420422046?hash=item3d1979dd9e:g:Gl8AAOSw1DtXKhrP"
                        }
                     ],
                     "Vip link":[  
                        {  
                           "text":"MINT 15\" Apple MacBook Pro Retina (2.3ghz i7, 8gb, 256gb) w/charger & Box",
                           "href":"http://www.ebay.com/itm/MINT-15-Apple-MacBook-Pro-Retina-2-3ghz-i7-8gb-256gb-w-charger-Box-/262420422046?hash=item3d1979dd9e:g:Gl8AAOSw1DtXKhrP"
                        }
                     ],
                     "Bold price":[  
                        {  
                           "text":"$1,000.00"
                        },
                        {  
                           "text":"$1,000.00"
                        }
                     ],
                     "Lvformat value":[  
                        {  
                           "text":"Buy It Now"
                        }
                     ]
                  }
               ]
            }
         ]
      },
   }
}

我使用http://pojo.sodhanalibrary.com/将其转换为java类。从那里我有一个组,ebayAPI,image_img,bold_price,vip_link和数据类。

我迷失在如何迭代“群组”中的不同项目以提取价格和链接。

例如,当我这样做时:         EbayAPI ebayAPI = gson.fromJson(json,EbayAPI.class);         Vip_link link = gson.fromJson(json,Vip_link.class);

    return new String[] {
            "From URL: " + ebayAPI.getUrl(),
            "Vip Link: " + link.getText() 
    }

我得到了结果:

From URL: http://www.ebay.com/sch/i.html?_sacat=0&LH_ItemCondition=3000&_nkw=macbook%20pro%2015%202012&rt=nc&LH_BIN=1&_trksid=p2045573.m1684
Vip Link: null

谢谢!

EbayAPI

public class EbayAPI {
    private Result result;

    private String url;

    public Result getResult ()
    {
        return result;
    }

    public void setResult (Result result)
    {
        this.result = result;
    }

    public String getUrl ()
    {
        return url;
    }

    public void setUrl (String url)
    {
        this.url = url;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [result = "+
                //result +
                ", url = "+url+"]";
    }
}

Vip Link

public class Vip_link
{
    private String text;

    private String href;

    public String getText ()
    {
        return text;
    }

    public void setText (String text)
    {
        this.text = text;
    }

    public String getHref ()
    {
        return href;
    }

    public void setHref (String href)
    {
        this.href = href;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [text = "+text+", href = "+href+"]";
    }
}

0 个答案:

没有答案