add jsonpathselector

pull/17/head
yihua.huang 12 years ago
parent 924d537da3
commit a339e4ab5c

@ -31,6 +31,11 @@
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.8.1</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -0,0 +1,53 @@
package us.codecraft.webmagic.selector;
import com.jayway.jsonpath.JsonPath;
import java.util.ArrayList;
import java.util.List;
/**
* @author code4crafter@gmail.com <br>
* Date: 13-8-12 <br>
* Time: 12:54 <br>
*/
public class JsonPathSelector implements Selector {
private String jsonPathStr;
private JsonPath jsonPath;
public JsonPathSelector(String jsonPathStr) {
this.jsonPathStr = jsonPathStr;
this.jsonPath = JsonPath.compile(jsonPathStr);
}
@Override
public String select(String text) {
Object object = jsonPath.read(text);
if (object == null) {
return null;
}
if (object instanceof List) {
List list = (List) object;
if (list != null && list.size() > 0) {
return list.iterator().next().toString();
}
}
return object.toString();
}
@Override
public List<String> selectList(String text) {
List<String> list = new ArrayList<String>();
Object object = jsonPath.read(text);
if (object == null) {
return list;
}
if (object instanceof List) {
return (List<String>)object;
} else {
list.add(object.toString());
}
return list;
}
}

@ -0,0 +1,49 @@
package us.codecraft.webmagic.selector;
import junit.framework.Assert;
import org.junit.Test;
import java.util.List;
/**
* @author code4crafter@gmai.com <br>
* Date: 13-8-12 <br>
* Time: 1:12 <br>
*/
public class JsonPathSelectorTest {
private String text = "{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99,\n" +
" \"isbn\": \"0-553-21311-3\"\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95\n" +
" }\n" +
" }\n" +
"}";
@Test
public void test() {
JsonPathSelector jsonPathSelector = new JsonPathSelector("$.store.book[*].author");
String select = jsonPathSelector.select(text);
List<String> list = jsonPathSelector.selectList(text);
Assert.assertNotNull(select);
Assert.assertNotNull(list);
jsonPathSelector = new JsonPathSelector("$.store.book[?(@.category == 'reference')]");
list = jsonPathSelector.selectList(text);
select = jsonPathSelector.select(text);
Assert.assertNotNull(list);
Assert.assertNotNull(select);
}
}
Loading…
Cancel
Save