2012-07-09 151 views
3

我想从URL解析JSON然后将数据添加到数组。 我正在使用GSON库。Android SDK:使用GSON从URL解析JSON

我的JSON格式如下:

[ 
    { 
     "img-src":"http://website.com/images/img1.png", 
     "URL":"http://google.com" 
    }, 
    { 
     "img-src":"http://website.com/images/img2.jpg", 
     "URL":"http://yahoo.com" 
    } 
] 

我要抢在一个单独的线程上面的数据,我有以下代码:

public class Async extends AsyncTask<String, Integer, Object>{ 

     @Override 
     protected String doInBackground(String... params) { 



      return null; 
     } 


    } 

我怎么去抓住每一个“img-src”和“URL”值?

回答

6

使用此方法以获取你的数据数组列表

public ArrayList<NewsItem> getNews(String url) { 
    ArrayList<NewsItem> data = new ArrayList<NewsItem>(); 

    java.lang.reflect.Type arrayListType = new TypeToken<ArrayList<NewsItem>>(){}.getType(); 
    gson = new Gson(); 

    httpClient = WebServiceUtils.getHttpClient(); 
    try { 
     HttpResponse response = httpClient.execute(new HttpGet(url)); 
     HttpEntity entity = response.getEntity(); 
     Reader reader = new InputStreamReader(entity.getContent()); 
     data = gson.fromJson(reader, arrayListType); 
    } catch (Exception e) { 
     Log.i("json array","While getting server response server generate error. "); 
    } 
    return data; 
} 

这应该是你应该如何申报您的ArrayList类型类(这里的NewsItem)

import com.google.gson.annotations.SerializedName; 
    public class NewsItem { 

@SerializedName("title") 
public String title; 

@SerializedName("content") 
public String title_details; 

@SerializedName("date") 
public String date; 

@SerializedName("featured") 
public String imgRawUrl; 


} 

这是WebSErvice Util类。

public class WebServiceUtils { 

public static HttpClient getHttpClient(){ 
     HttpParams httpParameters = new BasicHttpParams(); 
     // Set the timeout in milliseconds until a connection is established. 
     int timeoutConnection = 50000; 
     HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
     // Set the default socket timeout (SO_TIMEOUT) 
     // in milliseconds which is the timeout for waiting for data. 
     int timeoutSocket = 50000; 
     HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);   
     HttpClient httpclient = new DefaultHttpClient(httpParameters);   
     return httpclient; 
    } 

} 
+0

您将在哪里使用@SerializedName(“img-src”)和@SerializedName(“URL”)来填充您的类变量(这里的类是NewsItem) – 2012-07-09 13:49:30

+0

我在哪里获得WebServiceUtils类? – user1417302 2012-07-09 14:59:53

+0

我已经编辑了你的帖子,以便你可以看看WebServiceUtil类 – 2012-07-10 04:53:07

0

这就是我使用的代码(适合我)。

//Initialize the list 
Type listType = new TypeToken<ArrayList<YourObject>>(){}.getType(); 
//Parse 
List<YourObject> List= new Gson().fromJson(response, listType); 

YourObject应该是这样的:

public class Category { 
    private String URL; 
    private String img-src; 

    public Category(String URL, String img-src){ 
     this.URL= URL; 
     this.img-src= img-src; 
    } 
} 

问候。

Ps:通过这个,您将获得“YourObject”的列表。然后,你可以创建列出一个URL和其他与IMG-SRC

+0

我不知道如何获得这些值本身虽然,例如“IMG-SRC”的价值观应该被加入到一个列表和“URL”的价值观应该被添加到另一个列表 – user1417302 2012-07-09 13:42:24

+0

正确的,但我不知道为什么我需要另一个类,是不是可能/更快地下载数据并将其添加到ArrayList? – user1417302 2012-07-09 13:51:52

+0

@ user1417302我不确定。我有同样的问题,你和我发现没有另一种方式这样做。 – David 2012-07-09 13:54:44

0

在这个用户的Guid,你可以找到很多的例子:

https://sites.google.com/site/gson/gson-user-guide

+0

我看过这个用户指南,但不幸的是,对于初学者来说,有太多的信息 – user1417302 2012-07-09 13:47:56

+1

@ user1417302第一 - [我(代词)](http://en.wikipedia.org/wiki/I_(代词))。其次,你应该做你的工作。 “太多的信息”不是一个可以接受的理由。 – 2012-07-10 14:02:34