WebMagic为我们提供了JsonPathSelector用来提取JSON数据,具体用法参见下方的Demo 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.born2do.code;
import us.codecraft.webmagic.selector.JsonPathSelector;
public class WebMagicDemo01 {
public static void main(String[] args) { String json = "{\"mode\":\"xml\",\"code\":\" <dependency>\\n <groupId>org.springframework.boot</groupId>\\n <artifactId>spring-boot-configuration-processor</artifactId>\\n <optional>true</optional>\\n </dependency>\\n\\n\\n <build>\\n <plugins>\\n <plugin>\\n <groupId>org.springframework.boot</groupId>\\n <artifactId>spring-boot-maven-plugin</artifactId>\\n <configuration>\\n <excludes>\\n <exclude>\\n <groupId>org.springframework.boot</groupId>\\n <artifactId>spring-boot-configuration-processor</artifactId>\\n </exclude>\\n </excludes>\\n </configuration>\\n </plugin>\\n </plugins>\\n </build>\",\"heightLimit\":true,\"margin\":true,\"id\":\"Wptpj\"}\n"; System.out.println("原JSON数据:\n" + json); JsonPathSelector jsonPathSelector = new JsonPathSelector("$.code"); System.out.println("获取到的JSON数据中code的属性值:\n" + jsonPathSelector.select(json)); }
}
|
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| 原JSON数据: {"mode":"xml","code":" <dependency>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-configuration-processor</artifactId>\n <optional>true</optional>\n </dependency>\n\n\n <build>\n <plugins>\n <plugin>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-maven-plugin</artifactId>\n <configuration>\n <excludes>\n <exclude>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-configuration-processor</artifactId>\n </exclude>\n </excludes>\n </configuration>\n </plugin>\n </plugins>\n </build>","heightLimit":true,"margin":true,"id":"Wptpj"}
获取到的JSON数据中code的属性值: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build>
|
Related Issues not found
Please contact @born2do to initialize the comment