Hiển thị các bài đăng có nhãn net.sj.json. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn net.sj.json. Hiển thị tất cả bài đăng

Thứ Năm, 11 tháng 7, 2013

sử dụng JSON

kiến thức cơ bản về JSON:
Tiếng việt: http://www.qhonline.info/forum/showthread.php/4373-json-la-gi-json-lam-viec-nhu-the-nao-phan-1
w3school: http://www.w3schools.com/json/

Để sử dụng JSON trong java có thể dùng nhiều lib khác nhau như json.simple hoặc net.sf.json.
mình dùng cái thứ 2 net.sf.json. link : http://json-lib.sourceforge.net/
web hỗ trợ view json data online: http://www.jsoneditoronline.org/
ví dụ json data sau:
{
    "id": "s01",
    "name": "student1",
    "marks": [
        {
            "subject": "Toán",
            "mark": "9"
        },
        {
            "subject": "Hoá",
            "mark": "10"
        }
    ]
}
Parsing json data trên trong java:
String jsonStr = "{\"id\":\"s01\",\"name\":\"student1\",\"marks\":[{\"subject\":\"Toán\",\"mark\":\"9\"},{\"subject\":\"Hoá\",\"mark\":\"10\"}]}";
   JSONObject student = JSONObject.fromObject(jsonStr);
   System.out.println("ID: " + student.getString("id"));
   System.out.println("Name: " + student.getString("name"));
   System.out.println("Marks: ");
   JSONArray marks = student.getJSONArray("marks");
   Iterator it = marks.iterator();
   while(it.hasNext()) {
    JSONObject m = (JSONObject)it.next();
    System.out.println("subject: " + m.getString("subject"));
    System.out.println("mark: " + m.getString("mark"));
   }
Tạo một json data trong java:
JSONObject json = new JSONObject();
  json.put("id", "s01");
  json.put("name", "student1");
  System.out.println(json);
Nếu hiển thị trên web thì sử dụng javascript để phân tích json là tốt nhất. Javascript xem một json data là một object, và trong object đó có sẽ có nhiều object khác được định nghĩa.
 function start(temp){
  document.write(createTemplate(temp));
 }
 function doClick(id,obj) {
  getJson(id,obj); 
  $(obj).attr('onclick','expand(this)');
  $(obj).attr('src','images/minus.jpg');  
 
 }
 function expand(obj)
 {  
  if ($(obj).parent().find("ul").length>0) {   
   if($(obj).hasClass("expanded")) {
    $(obj).parent().children("ul").show();
    $(obj).removeClass("expanded");
    $(obj).attr('src','images/minus.jpg');
   } 
   else {
    $(obj).parent().children("ul").hide();
    $(obj).addClass("expanded");
    $(obj).attr('src','images/plus.png');
   }
  }
 }

 function createTemplate(temp) {
  var html;
  html = "
  • id: " + temp.id + ""; for ( var act in temp.actions) { html += "
    • action: " + temp.actions[act].action + "
    • "; html += "
    • type: "+ temp.actions[act].type +"
    • "; html += "
    • params
        "; for(var pa in temp.actions[act].params) { html += "
      • name: " + temp.actions[act].params[pa].name + "
      • "; html += "
      • data: " + temp.actions[act].params[pa].data + "
      • "; } html += "
    • "; } for ( var lnk in temp.links) { var x = temp.links[lnk].linkId; if(temp.links[lnk].hasOwnProperty("params") && temp.links[lnk].params.length > 0){ html += "
    • " + temp.links[lnk].params[0].name + ": " + temp.links[lnk].params[0].data; html += "
    •  link: " + x + "
    • "; } else{ html += "
    •  link: " + x + "
    • "; } } html += "
    "; return html; } function setId(id1,id2) { if(id1 == id2) return "none"; else return id2; }