-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathJSONParsingTest.java
More file actions
60 lines (51 loc) · 2.11 KB
/
JSONParsingTest.java
File metadata and controls
60 lines (51 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import model.LatestNews;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import parser.JArray;
import parser.JObject;
import parser.Parser;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2016/5/17.
*/
public class JSONParsingTest {
public static final String urlString = "http://news-at.zhihu.com/api/4/news/latest";
public static void main(String[] args) throws Exception {
long startTime = 0;
try {
String jsonString = new String(HttpUtil.get(urlString));
startTime = System.currentTimeMillis();
JSONObject latestNewsJSON = new JSONObject(jsonString);
String date = latestNewsJSON.getString("date");
JSONArray top_storiesJSON = latestNewsJSON.getJSONArray("top_stories");
LatestNews latest = new LatestNews();
List<LatestNews.TopStory> stories = new ArrayList<>();
for (int i = 0; i < top_storiesJSON.length(); i++) {
LatestNews.TopStory story = new LatestNews.TopStory();
story.setId(((JSONObject) top_storiesJSON.get(i)).getInt("id"));
story.setType(((JSONObject) top_storiesJSON.get(i)).getInt("type"));
story.setImage(((JSONObject) top_storiesJSON.get(i)).getString("image"));
story.setTitle(((JSONObject) top_storiesJSON.get(i)).getString("title"));
stories.add(story);
}
long endTime = System.currentTimeMillis();
double time = (double) (endTime - startTime) / 1000.0;
System.out.println("took " + time + "seconds.");
latest.setDate(date);
System.out.println("date: " + latest.getDate());
for (int i = 0; i < stories.size(); i++) {
System.out.println(stories.get(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}