/webmagic-core/src/main/java/us/codecraft/webmagic/selector/Json.java
Java | 57 lines | 38 code | 9 blank | 10 comment | 4 complexity | c342eaafcfbfa259225d56f9cf586775 MD5 | raw file
- package us.codecraft.webmagic.selector;
- import com.alibaba.fastjson.JSON;
- import us.codecraft.xsoup.XTokenQueue;
- import java.util.List;
- /**
- * parse json
- * @author code4crafter@gmail.com
- * @since 0.5.0
- */
- public class Json extends PlainText {
- public Json(List<String> strings) {
- super(strings);
- }
- public Json(String text) {
- super(text);
- }
- /**
- * remove padding for JSONP
- * @param padding padding
- * @return json after padding removed
- */
- public Json removePadding(String padding) {
- String text = getFirstSourceText();
- XTokenQueue tokenQueue = new XTokenQueue(text);
- tokenQueue.consumeWhitespace();
- tokenQueue.consume(padding);
- tokenQueue.consumeWhitespace();
- String chompBalanced = tokenQueue.chompBalancedNotInQuotes('(', ')');
- return new Json(chompBalanced);
- }
- public <T> T toObject(Class<T> clazz) {
- if (getFirstSourceText() == null) {
- return null;
- }
- return JSON.parseObject(getFirstSourceText(), clazz);
- }
- public <T> List<T> toList(Class<T> clazz) {
- if (getFirstSourceText() == null) {
- return null;
- }
- return JSON.parseArray(getFirstSourceText(), clazz);
- }
- @Override
- public Selectable jsonPath(String jsonPath) {
- JsonPathSelector jsonPathSelector = new JsonPathSelector(jsonPath);
- return selectList(jsonPathSelector,getSourceTexts());
- }
- }