2015-12-13 104 views
0

我有一些JSON格式的數據。我想在div上顯示它。以下不起作用。任何想法,我應該改變。以JSON格式追加數據

<div class="risktable"> </div>

$.ajax({ 
 
     contentType:'application/json', 
 

 
     data: {"sprint_start":"2015-12-13","sprint_end":"N.A.","available":[{"id":5,"name":"Thisun H","free_time":0.0,"possible":[]}],"assigns":[{"assignee_id":6,"assigned_to":"Isuru P","status":"OnTrack","hours_per_sprint":6,"limit_per_sprint":6.0,"limit_per_day":6.0,"tasks":[{"task_id":3,"task_name":"Schedule Servlet","project_id":1,"sprint_id":"2015-12-13","parent_id":0,"status":"New","assigned_to":"Isuru P","estimated_time":6,"remaining_time":6,"completed_ratio":0,"start_date":"2015-12-10","due_date":"2015-12-13","critical_task":true,"dependents":[]}]},{"assignee_id":7,"assigned_to":"Manoj D","status":"Risk","hours_per_sprint":6,"limit_per_sprint":5.0,"limit_per_day":5.0,"tasks":[{"task_id":4,"task_name":"Resource Servlet","project_id":1,"sprint_id":"2015-12-13","parent_id":0,"status":"New","assigned_to":"Manoj D","estimated_time":6,"remaining_time":0,"completed_ratio":0,"start_date":"2015-12-10","due_date":"2015-12-13","critical_task":true,"dependents":[]}]},{"assignee_id":5,"assigned_to":"Thisun H","status":"OnTrack","hours_per_sprint":0,"limit_per_sprint":6.0,"limit_per_day":6.0,"tasks":[{"task_id":1,"task_name":"Redmine Connection","project_id":1,"sprint_id":"2015-12-13","parent_id":0,"status":"New","assigned_to":"Thisun H","estimated_time":3,"remaining_time":0,"completed_ratio":100,"start_date":"2015-12-07","due_date":"2015-12-09","critical_task":true,"dependents":[2,3,4]},{"task_id":2,"task_name":"Risk Servlet","project_id":1,"sprint_id":"2015-12-13","parent_id":0,"status":"New","assigned_to":"Thisun H","estimated_time":10,"remaining_time":0,"completed_ratio":100,"start_date":"2015-12-10","due_date":"2015-12-12","critical_task":false,"dependents":[]}]}],"suggestions":[{"dev_id":5,"dev_name":"Thisun H","task_id":4,"task_name":"Resource Servlet"}]}, 
 

 
      success: function(data){ 
 
     
 
      $.each(data.data, function(i,value) { 
 
       $('.risktable').append(
 
       '<div>'+'<p>'+ value.assigns.assignee_id + '</p>' 
 
       
 
       +'</div>' 
 

 
       ); 
 
      }); 
 

 
      }  
 
     });

+0

可能有很多東西,比如什麼風格的'.risktable'?你能否在成功處理程序中打印出對象'data'? –

+0

可能是一個div,只需要打印一個元素。 – mars

回答

1

assigns財產的JSON對象是一個數組。你需要循環遍歷這些項目並構建你的div並添加它。

$.each(data.assigns, function(i,value) { 
    var item='<div>'+'<p>'+ value.assignee_id + '</p></div>'; 
    $('.risktable').append(item); 
}); 

Here是一個工作示例。

+0

這種格式有什麼問題! 'code' 變種項= ' '+ ' '+ value.assignee_id +' '+ ' '+ value.assigned_to +' '+ ' '+ value.status +'' + ''; – mars

+0

應該正常工作。檢查這個http://jsbin.com/xahuxiyuba/edit?html,js輸出 – Shyju

1

如果您擁有的json數據沒有從外部獲取(即您已經有json對象定義&值),則不需要執行ajax方法調用。 Ajax用於提出外部請求。

或者,您可以將您的json對象存儲爲一個通用變量,並通過它進行解析(假設您使用的是jQuery),如下所示。

var mock_data = {"sprint_start": "2015-12-13","sprint_end":"N.A.","available": [{"id":5,"name":"Thisun H","free_time":0.0,"possible":[]}],"assigns":[{"assignee_id":6,"assigned_to":"Isuru P","status":"OnTrack","hours_per_sprint":6,"limit_per_sprint":6.0,"limit_per_day":6.0,"tasks":[{"task_id":3,"task_name":"Schedule Servlet","project_id":1,"sprint_id":"2015-12-13","parent_id":0,"status":"New","assigned_to":"Isuru P","estimated_time":6,"remaining_time":6,"completed_ratio":0,"start_date":"2015-12-10","due_date":"2015-12-13","critical_task":true,"dependents":[]}]},{"assignee_id":7,"assigned_to":"Manoj D","status":"Risk","hours_per_sprint":6,"limit_per_sprint":5.0,"limit_per_day":5.0,"tasks":[{"task_id":4,"task_name":"Resource Servlet","project_id":1,"sprint_id":"2015-12-13","parent_id":0,"status":"New","assigned_to":"Manoj D","estimated_time":6,"remaining_time":0,"completed_ratio":0,"start_date":"2015-12-10","due_date":"2015-12-13","critical_task":true,"dependents":[]}]},{"assignee_id":5,"assigned_to":"Thisun H","status":"OnTrack","hours_per_sprint":0,"limit_per_sprint":6.0,"limit_per_day":6.0,"tasks":[{"task_id":1,"task_name":"Redmine Connection","project_id":1,"sprint_id":"2015-12-13","parent_id":0,"status":"New","assigned_to":"Thisun H","estimated_time":3,"remaining_time":0,"completed_ratio":100,"start_date":"2015-12-07","due_date":"2015-12-09","critical_task":true,"dependents":[2,3,4]},{"task_id":2,"task_name":"Risk Servlet","project_id":1,"sprint_id":"2015-12-13","parent_id":0,"status":"New","assigned_to":"Thisun H","estimated_time":10,"remaining_time":0,"completed_ratio":100,"start_date":"2015-12-10","due_date":"2015-12-12","critical_task":false,"dependents":[]}]}],"suggestions":[{"dev_id":5,"dev_name":"Thisun H","task_id":4,"task_name":"Resource Servlet"}]}; 

$(mock_data.assigns).each(function(){ 
    $('.risktable').append('<div><p>'+this.assignee_id+'</p></div>'); 
}); 

您可以在這裏看到的樣品: http://codepen.io/team/anthro-web/pen/yeYobN