json学习 我相信一定对大家有帮助

http://hi.baidu.com/coolcooldool/blog/item/a737888fdc485cf0513d9287.html     json所有知识

http://yxgyh.iteye.com/blog/392657
http://www.iteye.com/topic/295083
http://www.cnblogs.com/known/archive/2009/06/08/1417128.html
自己练习的例子:
Model 对象
                   private Integer id;
                private String name;
                private String status;
                private Type type;
Type 对象
                   private Integer id;
                private String typeName;
action层代码
                     Type type = new Type();
                  type.setId(2);
                  type.setTypeName("yyyy");

List<Model> list = new ArrayList<Model>();
Model m = new Model(1,"haiwei", "true");
m.setType(type);
list.add(m);
Model m1 = new Model(2,"guoyu", "false");
m1.setType(type);
list.add(m1);

JSONArray ja = JSONArray.fromObject(list);
System.out.println(ja.toString());
MessageUtils.outputJSONResult("{[ list:"+ja.toString()+"}", response);  结果返回页面

jsp文件
<script type="text/javascript" src="${pageContext.request.contextPath}/js/common/jquery-1.3.1.js"></script>
  </head>
  <script type="text/javascript">
  $(document).ready(function () {
     $.getJSON("${pageContext.request.contextPath}/shop/main/getJson.do", function(json){
var html = "";
$.each(json. list, function(i, n){
    alert(i);
html += "<option value=" + n.id + ">" + n.name +"--"+n.status+ "</option>";
var array = n.type;
alert(array['id']+array['typeName']);
});
alert(html);
});
  })
  </script>

经过刚才的练习才发现,json对象里面的list必须和封装中list必须一致,否则找不见对象,我在公司那个方式是不是也是因为这个才没出现结果的呢,明天我得试试去

你可能感兴趣的:(html,json,jsp,Blog)