對json串的解析 fastjson gson ajax非同步重新整理
json 是輕量級的傳輸文字格式,解析簡單,支援各種語言 json語法特點:以鍵值對的形式儲存 資料之間用逗號分隔 {}是一個物件 []是一個數據(集合) fastjson解析 是阿里出的對json處理
eg: [{“sid”:1 , “sname”:”leif” , “salary”:3000},{“sid”:2 , “sname”:”Suny” , “salary”:4000}]
String studentJson="{\"uid\":1,\"uname\":\"小彬彬\",\"uproject\":\"Mathmatics\"}"; //手動解析json, //第一個引數是需要解析的json字串,第二個引數是泛型 Student student=JSON.parseObject(studentJson, Student.class); System.out.println(student.getUid()+"\t"+student.getUname()+"\t"+student.getUproject()); String jsonArray="[{\"uid\":1,\"uname\":\"小彬彬\",\"uproject\":\"Mathmatics\"}," + "{\"uid\":2,\"uname\":\"小小彬\",\"uproject\":\"Mathmatics\"}," + "{\"uid\":3,\"uname\":\"小可愛\",\"uproject\":\"Mathmatics\"}]"; //解析集合 List<Student> list=JSON.parseArray(jsonArray,Student.class); for (Student student:list) { System.out.println(student.getUid()+"\t"+student.getUname()+"\t"+student.getUproject()); }
//把物件改成一個json串
String studentJson="{“uid”:1,“uname”:“小彬彬”,“uproject”:“Mathmatics”}";
Student student=JSON.parseObject(studentJson, Student.class);
String str=JSON.toJSONString(student);
System.out.println(str);
//物件裡有物件
String str="[\n" +
" {\n" +
" date: “Sep 18 2016”,\n" +
" title: “御劍飛仙翱於九天”,\n" +
" imgSrc: “/images/post/crab.png”,\n" +
" avatar: “/images/avatar/1.png”,\n" +
" content: “金庸書的一大特點就是朝代越早武功越高,天龍的朝代算是最早的 因此天龍裡面的功夫都比其他書裡面的厲害,就連降龍十八掌也比射鵰三步曲裡面的厲害,天龍裡面一陽指不是非常厲害,但是到了射鵰就是相當強大的功夫了。但是後面幾部書裡面還是有一些奇人練的一些奇功超越了前人。”,\n" +
" reading: “112”,\n" +
" collection: “96”,\n" +
" headImgSrc: “/images/post/crab.png”,\n" +
" author: “林白衣”,\n" +
" dateTime: “24小時前”,\n" +
" detail: “菊黃蟹正肥,品嚐秋之味。徐志摩把“看初花的荻蘆”和“到樓外樓吃蟹”並列為秋天來杭州不能錯過的風雅之事;用林妹妹的話講是“螯封嫩玉雙雙滿,殼凸紅脂塊塊香”;在《世說新語》裡,晉畢卓更是感嘆“右手持酒杯,左手持蟹螯,拍浮酒船中,便足了一生矣。”漫漫人生長路,美食與愛豈可辜負?於是作為一個吃貨,突然也很想回味一下屬於我的味蕾記憶。記憶中的秋蟹,是家人的味道,瀰漫著濃濃的親情。\n\n是誰來自山川湖海,卻囿於晝夜,廚房與愛? 是母親,深思熟慮,聰明耐心。吃蟹前,總會拿出幾件工具,煞有介事而樂此不疲。告訴我們螃蟹至寒,需要佐以薑茶以祛寒,在配備的米醋小碟裡,亦添入薑絲與紫蘇,前者驅寒後者增香。泡好菊花茶,歲月靜好,我們靜等。”,\n" +
" postId: 0,\n" +
" music: {\n" +
" url: “
//用Gson進行集合解析 TypeToken轉換器 public static void main(String[] args) { String studentJson="[{“uid”:1,“uname”:“小彬彬”,“uproject”:“Mathmatics”}," + “{“uid”:2,“uname”:“小小彬”,“uproject”:“Mathmatics”},” + “{“uid”:3,“uname”:“小可愛”,“uproject”:“Mathmatics”}]”; //用Gson進行集合解析 TypeToken轉換器 Gson gson=new Gson(); List list= gson.fromJson(studentJson,new TypeToken<List>(){}.getType()); for (Student student1:list) { System.out.println(student1.getUid()+"\t"+student1.getUname()+"\t"+student1.getUproject()); }
**//把集合轉換成Json串**
String str=gson.toJson(list);
System.out.println(str);
}
ajax 非同步處理, 區域性重新整理 用於web中 index.jsp
//get方法傳送時 <%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
//post方法傳送時 <%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
UserServlet.java
public class UserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
String userName= req.getParameter("name");
boolean flag=false;
if (userName.equals("ajax")){
flag=true;
}else{
flag=false;
}
PrintWriter out= resp.getWriter();
out.print(flag);
out.flush();
out.close();
}