PageRenderTime 66ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/ode/TargetCrawler/src/main/java/org/tomstools/crawler/parser/JSONParser.java

https://github.com/lotomer/tomstools
Java | 73 lines | 38 code | 12 blank | 23 comment | 2 complexity | 6a78e82e014f8f69377a5844f323281c MD5 | raw file
  1. /**
  2. * copyright (a) 2010-2014 tomstools.org. All rights reserved.
  3. */
  4. package org.tomstools.crawler.parser;
  5. import org.tomstools.crawler.common.Element;
  6. import org.tomstools.crawler.common.ElementProcessor;
  7. import com.alibaba.fastjson.JSON;
  8. import com.alibaba.fastjson.JSONObject;
  9. /**
  10. * json解析器
  11. * @author admin
  12. * @date 2014年3月12日
  13. * @time 下午8:47:00
  14. * @version 1.0
  15. */
  16. public class JSONParser implements Parser {
  17. @Override
  18. public Element parse(String content, String param) {
  19. JSONObject obj = JSON.parseObject(content);
  20. if (null != obj){
  21. return new JsonElement(obj.getJSONObject(param));
  22. }else{
  23. return null;
  24. }
  25. }
  26. // public void parse(String content, Map<String, String> params, ContentHandle handle) {
  27. // JSONObject obj = JSON.parseObject(content);
  28. // if (null != obj){
  29. // for (Entry<String, String> entry : params.entrySet()) {
  30. // handle.handle(entry.getKey(), new JsonElement(obj.getJSONObject(entry.getValue())));
  31. // }
  32. // }
  33. // }
  34. public static class JsonElement implements Element{
  35. private JSONObject obj;
  36. /**
  37. * @param obj
  38. * @since 1.0
  39. */
  40. public JsonElement(JSONObject obj) {
  41. super();
  42. this.obj = obj;
  43. }
  44. @Override
  45. public String getCode() {
  46. return obj.toJSONString();
  47. }
  48. @Override
  49. public String getAttribute(String attributeName) {
  50. return obj.getString(attributeName);
  51. }
  52. @Override
  53. public String getText() {
  54. return obj.toString();
  55. }
  56. @Override
  57. public void select(String cssQuery, ElementProcessor processor) {
  58. // TODO Auto-generated method stub
  59. }
  60. }
  61. }