Problem:从网址 http://quotes.money.163.com/hs/service/marketradar_ajax.php?host=http%3A%2F%2Fquotes.money.163.com%2Fhs%2Fservice%2Fmarketradar_ajax.php&page=2&query=STYPE%3AEQA&types=&count=28&type=query&order=desc 获得了如下的json文本:
{"page":2,"count":28,"total":10085,"pagecount":361,"time":"2020-05-03 12:09:45","list":[{"CODE":"0600256","SYMBOL":"600256","NAME":"\u5e7f\u6c47\u80fd\u6e90","PRICE":2.52,"PERCENT":0.028571,"NUMBER":"7,204\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0603536","SYMBOL":"603536","NAME":"\u60e0\u53d1\u98df\u54c1","PRICE":13.34,"PERCENT":-0.023426,"NUMBER":"1,876\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0600399","SYMBOL":"600399","NAME":"ST\u629a\u94a2","PRICE":4.21,"PERCENT":-0.029954,"NUMBER":"7,660\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0603601","SYMBOL":"603601","NAME":"\u518d\u5347\u79d1\u6280","PRICE":12.16,"PERCENT":-0.055901,"NUMBER":"6,580\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0603538","SYMBOL":"603538","NAME":"\u7f8e\u8bfa\u534e","PRICE":34.53,"PERCENT":-0.032502,"NUMBER":"1,803\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5916\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u5356\u5355","TYPE_COLOR":-1},{"CODE":"0601169","SYMBOL":"601169","NAME":"\u5317\u4eac\u94f6\u884c","PRICE":4.98,"PERCENT":0.004032,"NUMBER":"7,031\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0600547","SYMBOL":"600547","NAME":"\u5c71\u4e1c\u9ec4\u91d1","PRICE":36.29,"PERCENT":0.004151,"NUMBER":"5,310\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0603012","SYMBOL":"603012","NAME":"\u521b\u529b\u96c6\u56e2","PRICE":7.18,"PERCENT":0.008427,"NUMBER":"6,148\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0600126","SYMBOL":"600126","NAME":"\u676d\u94a2\u80a1\u4efd","PRICE":7.92,"PERCENT":0.077551,"NUMBER":"5,855\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0603825","SYMBOL":"603825","NAME":"\u534e\u626c\u8054\u4f17","PRICE":24.91,"PERCENT":0.084458,"NUMBER":"1,044\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0603668","SYMBOL":"603668","NAME":"\u5929\u9a6c\u79d1\u6280","PRICE":11.59,"PERCENT":-0.047658,"NUMBER":"4,911\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0601111","SYMBOL":"601111","NAME":"\u4e2d\u56fd\u56fd\u822a","PRICE":7.25,"PERCENT":0.058394,"NUMBER":"7,266\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0600568","SYMBOL":"600568","NAME":"\u4e2d\u73e0\u533b\u7597","PRICE":1.35,"PERCENT":0.007463,"NUMBER":"6,446\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0601975","SYMBOL":"601975","NAME":"\u62db\u5546\u5357\u6cb9","PRICE":2.67,"PERCENT":0.003759,"NUMBER":"13,883\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0688396","SYMBOL":"688396","NAME":"\u534e\u6da6\u5fae","PRICE":37.83,"PERCENT":0.08832,"NUMBER":"3,766\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0603533","SYMBOL":"603533","NAME":"\u638c\u9605\u79d1\u6280","PRICE":34,"PERCENT":-0.054242,"NUMBER":"4,391\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0688116","SYMBOL":"688116","NAME":"\u5929\u5948\u79d1\u6280","PRICE":40.01,"PERCENT":0.136648,"NUMBER":"786\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0688166","SYMBOL":"688166","NAME":"\u535a\u745e\u533b\u836f","PRICE":48,"PERCENT":0.046436,"NUMBER":"677\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0603815","SYMBOL":"603815","NAME":"\u4ea4\u5efa\u80a1\u4efd","PRICE":21,"PERCENT":0.053688,"NUMBER":"1,947\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0601952","SYMBOL":"601952","NAME":"\u82cf\u57a6\u519c\u53d1","PRICE":10.17,"PERCENT":-0.024928,"NUMBER":"6,356\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0601377","SYMBOL":"601377","NAME":"\u5174\u4e1a\u8bc1\u5238","PRICE":6.03,"PERCENT":0.018581,"NUMBER":"7,780\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0601872","SYMBOL":"601872","NAME":"\u62db\u5546\u8f6e\u8239","PRICE":6.78,"PERCENT":-0.042373,"NUMBER":"15,050\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0601038","SYMBOL":"601038","NAME":"\u4e00\u62d6\u80a1\u4efd","PRICE":7.04,"PERCENT":-0.015385,"NUMBER":"8,200\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0600016","SYMBOL":"600016","NAME":"\u6c11\u751f\u94f6\u884c","PRICE":5.89,"PERCENT":0.003407,"NUMBER":"10,004\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0688015","SYMBOL":"688015","NAME":"\u4ea4\u63a7\u79d1\u6280","PRICE":49.26,"PERCENT":0.05032,"NUMBER":"1,093\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0603719","SYMBOL":"603719","NAME":"\u826f\u54c1\u94fa\u5b50","PRICE":67.38,"PERCENT":-0.005021,"NUMBER":"1,854\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0688222","SYMBOL":"688222","NAME":"\u6210\u90fd\u5148\u5bfc","PRICE":35.01,"PERCENT":0.029101,"NUMBER":"513\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1},{"CODE":"0600556","SYMBOL":"600556","NAME":"\u5929\u4e0b\u79c0","PRICE":16.68,"PERCENT":0.005425,"NUMBER":"4,471\u624b","TITLE":"\u51fa\u73b0\u5927\u4e8e5000\u624b\u6216\u548c\u6d41\u901a\u76d8\u7684\u6bd4\u503c\u5927\u4e8e0.1%\u7684\u5185\u76d8\u6210\u4ea4","TIME":"15:00:05","TYPE":"\u5927\u4e70\u5355","TYPE_COLOR":1}]}
需要从中获得pagecount的文本以及list中每个股票的NAME和CODE。
Solution:使用jackson完成此任务。
Steps:
1.引入jackson依赖
<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
2.解析
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);// json就是上面的大段待解析文本
pageCount = Integer.parseInt(node.get("pagecount").asText());// 取pagecount
JsonNode listNode=node.path("list");
Iterator<JsonNode> iterator = listNode.elements(); // 准备遍历list节点
while (iterator.hasNext()) {
JsonNode stock = iterator.next();
String code=stock.get("CODE").asText(); // 取code
String name=stock.get("NAME").asText(); // 取name
Stock s=new Stock(idx++,code,name);
stockList.add(s);
//System.out.print(code + " "+name);
}
--2020年5月3日--
参考文档:https://blog.csdn.net/wang_snake/article/details/93719178
来源:oschina
链接:https://my.oschina.net/u/4271739/blog/4264867