62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.text.html.parser.Entity;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonTest {
private ObjectMapper objectMapper = new ObjectMapper();
private static final String json = "{" +
"\"ajaxResult\":{\"code\":1,\"message\":\"有数据\"}," +
"\"chapterlist\":{\"asc\":0," +
"\"chapterCount\":10," +
"\"chapters\":" +
"[{\"chapterId\":25492068,\"chapterName\":\"正文 第一章:地震\",\"level\":0,\"orderNum\":1,\"tomeId\":1233505}," +
"{\"chapterId\":25686707,\"chapterName\":\"正文 第二章:地下\",\"level\":0,\"orderNum\":2,\"tomeId\":1233505}," +
"{\"chapterId\":25740084,\"chapterName\":\"正文 第三章:传承\",\"level\":0,\"orderNum\":3,\"tomeId\":1233505}]," +
"\"pageNum\":1," +
"\"pageSize\":10}}";
public void readJson2Map(){
try {
Jsonp jsonData = objectMapper.readValue(json, Jsonp.class); //一直显示这句报错,但是没有抛出具体异常
System.out.println(jsonData.getAjaxResult().getCode());
// System.out.println(jsonData);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
JacksonTest test = new JacksonTest();
test.readJson2Map();
}
}
public class Jsonp {
private AjaxResult ajaxResult;
private ChapterList chapterList;
public AjaxResult getAjaxResult() {
return ajaxResult;
}
public void setAjaxResult(AjaxResult ajaxResult) {
this.ajaxResult = ajaxResult;
}
public ChapterList getChapterList() {
return chapterList;
}
public void setChapterList(ChapterList chapterList) {
this.chapterList = chapterList;
}
}
import java.util.List;
public class ChapterList {
private int asc;
private int chapterCount;
private List<Chapters> chapters;
private int pageNum;
private int pageSize;
public int getAsc() {
return asc;
}
public void setAsc(int asc) {
this.asc = asc;
}
public int getChapterCount() {
return chapterCount;
}
public void setChapterCount(int chapterCount) {
this.chapterCount = chapterCount;
}
public List<Chapters> getChapters() {
return chapters;
}
public void setChapters(List<Chapters> chapters) {
this.chapters = chapters;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
public class Chapters {
private int chapterId;
private String chapterName;
private int level;
private int orderNum;
private int tomeId;
public int getChapterId() {
return chapterId;
}
public void setChapterId(int chapterId) {
this.chapterId = chapterId;
}
public String getChapterName() {
return chapterName;
}
public void setChapterName(String chapterName) {
this.chapterName = chapterName;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getOrderNum() {
return orderNum;
}
public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}
public int getTomeId() {
return tomeId;
}
public void setTomeId(int tomeId) {
this.tomeId = tomeId;
}
}
public class AjaxResult {
private int code;
private String message;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String toString(){
return this.code +" "+ this.message;
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String s="<a>目录<span class=\"sf\">(共1804章)</span></a>";
Pattern p=Pattern.compile(">\\(共(\\d+)章\\)<");
Matcher m=p.matcher(s);
if(m.find()){
System.out.println(m.group(1));
}
}
}
运行结果:
1804
String s = "共1008章";
s = s.substring(1, s.length() - 1);
System.out.println(s);
这样就搞定了,是不是很轻松愉快又不用折腾啥正则呢?Pattern p=Pattern.compile("(\\d+)");
Matcher m=p.matcher("第234章");
while(m.find()){
System.out.println(m.group());
}