Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webmagic
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
沈俊林
webmagic
Commits
a339e4ab
Commit
a339e4ab
authored
Aug 12, 2013
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add jsonpathselector
parent
924d537d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
107 additions
and
0 deletions
+107
-0
pom.xml
webmagic-extension/pom.xml
+5
-0
JsonPathSelector.java
...java/us/codecraft/webmagic/selector/JsonPathSelector.java
+53
-0
JsonPathSelectorTest.java
.../us/codecraft/webmagic/selector/JsonPathSelectorTest.java
+49
-0
No files found.
webmagic-extension/pom.xml
View file @
a339e4ab
...
...
@@ -31,6 +31,11 @@
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
</dependency>
<dependency>
<groupId>
com.jayway.jsonpath
</groupId>
<artifactId>
json-path
</artifactId>
<version>
0.8.1
</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
webmagic-extension/src/main/java/us/codecraft/webmagic/selector/JsonPathSelector.java
0 → 100644
View file @
a339e4ab
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
;
}
}
webmagic-extension/src/test/java/us/codecraft/webmagic/selector/JsonPathSelectorTest.java
0 → 100644
View file @
a339e4ab
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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment