package json;import java.util.ArrayList;
import java.util.List;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;import bean.Stock;publicclassJsonTest {
/** 将java对象转换成json字符串*/publicstaticvoidtest1() throws JsonProcessingException{Stock s = new Stock();s.setCode("600877");s.setName("中国嘉陵");s.setPrice(10);//使用jackson提供的工具来转换ObjectMapper om = new ObjectMapper();String jsonStr = om.writeValueAsString(s);System.out.println(jsonStr);}publicstaticvoidtest2() throws JsonProcessingException{List<Stock> stocks = new ArrayList<Stock>();for(int i = 0; i < 3; i ++){Stock s = new Stock();s.setCode("60087" + i);s.setName("中国嘉陵" + i);s.setPrice(10 + i);stocks.add(s);}ObjectMapper om = new ObjectMapper();String jsonStr = om.writeValueAsString(stocks);System.out.println(jsonStr);}publicstaticvoidmain(String[] args) throws JsonProcessingException {test2();}}
web
ActionServlet.java
package web;import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.fasterxml.jackson.databind.ObjectMapper;import bean.Stock;public class ActionServlet extends
HttpServlet{public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();String uri = request.getRequestURI();String action = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));System.out.println("action:" + action);if("/quoto".equals(action)){//返回几只股票的信息给客户端List<Stock> stocks = new ArrayList<Stock>();Random r = new Random();for(int i = 0; i < 8; i ++){
Stock s = new Stock();s.setCode("60087" + r.nextInt(10));s.setName("中国嘉陵" + r.nextInt(10));s.setPrice(r.nextInt(1000));stocks.add(s);}ObjectMapper om = new ObjectMapper();String jsonStr = om.writeValueAsString(stocks);System.out.println(jsonStr);out.println(jsonStr);}else if("/getNumber".equals(action)){//返回一个随机整数Random r = new Random();int number = r.nextInt(1000);System.out.println(number);out.println(number);}}
}
webapp
json.html
<!DOCTYPE html><html><head><metacharset="UTF-8"><scripttype="text/javascript">/*使用json语法表示对象*/functionf1(){
var obj = {
"code":"600877","name":"中国嘉陵","price":10};alert(obj.name);}functionf2(){
var obj = {
"company":"达内科技","address":{"city":"北京","street":"北三环西路","room":2046}};alert(obj.address.street);}functionf3(){
var arr = [{
"name":"Sally","age":22},{
"name":"Eric","age":32}];alert(arr[1].name);}//将json字符串转换成javascript对象functionf4(){
var str = '{"name":"Sally","age":22}';//调用javascript内置对象JSON提供的方法//来做转换。var obj = JSON.parse(str);alert(obj.name);}functionf5(){
var str = '[{"name":"Sally","age":22},' + '{"name":"Eric","age":32}]';//转换完后,得到的是一个数组。var arr = JSON.parse(str);alert(arr[1].name);} </script></head><bodystyle="font-size:30px;"><ahref="javascript:f5();">ClickMe</a></body></html>