我正在尝试使用gson将这个返回的JSON转换成某种形式
数据结构使我可以提取有用的数据.
例如:
http://search.twitter.com/search.json?q=test&rpp=1
返回:
{
"completed_in":0.028,
"max_id":196386333906837504,
"max_id_str":"196386333906837504",
"next_page":"?page=2&max_id=196386333906837504&q=test&rpp=1",
"page":1,
"query":"test",
"refresh_url":"?since_id=196386333906837504&q=test",
"results":[
{
"created_at":"Sat, 28 Apr 2012 23:52:05 +0000",
"from_user":"della_ky",
"from_user_id":525641596,
"from_user_id_str":"525641596",
"from_user_name":"kydella modeste",
"geo":null,
"id":196386333906837504,
"id_str":"196386333906837504",
"iso_language_code":"en",
"metadata":{
"result_type":"recent"
},
"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/2159990525\/webcam-toy-photo3_20_2__normal.jpg",
"profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/2159990525\/webcam-toy-photo3_20_2__normal.jpg",
"source":"<a href="http:\/\/mobile.twitter.com" rel="nofollow">Mobile Web<\/a>",
"text":"RT @Y__U__NOOO: #SongsIKnowOffByHeart ALL SONGS I LISTEN TO. BRAIN, Y U NO REMEMBER TEST ANSWERS LIKE THAT?!?",
"to_user":null,
"to_user_id":null,
"to_user_id_str":null,
"to_user_name":null
}
],
"results_per_page":1,
"since_id":0,
"since_id_str":"0"
}
最终,我希望能够输出一个推文列表
发件人的姓名和推文的日期/时间.
我已经阅读了gson文档,但它已经过了我的脑海
说实话 – 我有很多新概念.
我是否需要定义一个完全映射到结构的类
JSON以便然后填充该类的实例?如果是这样的话
似乎非常不灵活/费力.理想情况下,我正在寻找一些东西
它将处理任何形式的JSON,并给我一个我可以使用的结构
自动…
有人能给我一些指示吗?对此更新 – 更多
详细的,用最少的音节的话来说就更好了!
更新 – 感谢我已经对此做出的回应,我已经开始将一个类放在一起来捕获twitter JSON.但是,由于JSON有一个嵌入的对象ArrayList,我有点挣扎……到目前为止,我有
public class tweetData {
private double completed_in;
private long max_id;
private long max_id_str;
private String next_page;
private int page;
private String query;
private String refresh_url;
private List<tweetDetails> tweets = new ArrayList<tweetDetails>();
}
和
public class tweetDetails {
private String created_at;
private String from_user;
private long from_user_id;
private long from_user_id_str;
private String from_user_name;
private String geo;
private long id;
private long id_str;
private String iso_language_code;
// "metadata":
// {
// "result_type":"recent"
// },
private String profile_image_url;
private String profile_image_url_https;
private String source;
private String text;
private String to_user;
private String to_user_id;
private String to_user_id_str;
private String to_user_name;
}
我正在实例化
URI uri = new URI("http", "search.twitter.com", "/search.json", "q="+ searchTerms + "&rrp=" + RRP, null);
URL twitterSearch = uri.toURL();
URLConnection yc = twitterSearch.openConnection();
JsonReader reader = new JsonReader(new InputStreamReader(yc.getInputStream()));
Gson gson = new Gson();
tweetData data = gson.fromJson(reader, tweetData.class);
System.out.println(data);
基本名称:正确填充值但ArrayList不正确填充.
tweetData : 0.17196614959919140865196614959919140865?page=2&max_id=196614959919140865&q=test1test?since_id=196614959919140865&q=testSIZE 0[]
所以,我还在苦苦挣扎 – 任何更多的提示非常感谢!
蒂亚,
汤姆
解决方法:
Do I need to define a class which maps exactly to the structure of the JSON in order to then populate an instance of that class? If so this seems very inflexible/laborious.
是. GSON是一个可用于将Java对象转换为其JSON表示的库.它还可用于将JSON字符串转换为等效的Java对象.这非常强大,因为您可以从JSON表示自动实例化Java对象.假设您的JSON没有更改其结构,您只需要定义一次适当的Java对象表示.
Ideally I’m looking for something which will handle JSON in any form and give me a structure I can use automatically…
但是,如果您不想进行自动序列化/反序列化,那么请尝试查看更简单的库,例如java.net/projects/jsonp.
您只需查询键即可从中提取内容:
final JSONObject json = new JSONObject(theJsonString);
final String id = json.getString("max_id");
final JSONArray results = json.getJSONArray("results");
final String user = results.getJSONObject(2).getString("from_user");