2013-02-25 38 views
0

我正在嘗試這種方式,因爲在這裏搜索不同的帖子......但無法獲得有關如何從JSON對象或Array中獲取數據/值的概念如何從Json objecj或Array中檢索數據並使用jquery更新下拉列表

我正在使用Gson將我的ArrayList轉換爲JSON。

response.setContentType("application/json"); 
response.setCharacterEncoding("UTF-8"); 
Gson gson=new Gson(); 
JsonObject myobj=new JsonObject(); 
JsonElement element=gson.toJsonTree(owningOrgRecords); 
    myobj.add("owningorg", element); 
    out.print(myobj.toString()); 
//i tried this too String teamJSON=gson.toJson(owningOrgRecords); and out.print(teamJSON). 

出把我得到當我使用myobj.toString()

JSON對象

{ 
"owningorg": [ 
    { 
     "owning_org_id": 157, 
     "owning_org_desc": "FirstOwning Org", 
     "team": "SSSB Team", 
     "created_on": "TodayDate", 
     "created_by": "Adminuser" 
    }, 
    { 
     "owning_org_id": 158, 
     "owning_org_desc": "SecondOwning Org", 
     "team": "SSSB Team", 
     "created_on": "TodayDate", 
     "created_by": "Adminuser" 
    }, 
    { 
     "owning_org_id": 159, 
     "owning_org_desc": "ThirdOwning Org", 
     "team": "SSSB Team", 
     "created_on": "TodayDate", 
     "created_by": "Adminuser" 
    } 
] 

}

JSON陣列

[ 
{ 
    "owning_org_id": 157, 
    "owning_org_desc": "FirstOwning Org", 
    "team": "SSSB Team", 
    "created_on": "TodayDate", 
    "created_by": "Adminuser" 
}, 
{ 
    "owning_org_id": 158, 
    "owning_org_desc": "SecondOwning Org", 
    "team": "SSSB Team", 
    "created_on": "TodayDate", 
    "created_by": "Adminuser" 
}, 
{ 
    "owning_org_id": 159, 
    "owning_org_desc": "ThirdOwning Org", 
    "team": "SSSB Team", 
    "created_on": "TodayDate", 
    "created_by": "Adminuser" 
} 

]

我目前擁有的Java腳本。

$.ajax({ 
     type:"POST", 
     dataType:"json", 
     url:"myServlet", 
     cache:false, 
     data:a, 
     success:function(html){ 
      console.debug(html); 
      var jsonList=html.owningorg; 
      alert(jsonList.owningorg.lenght); 
      $("#owningorg").removeAttr("disabled"); 
      $("#owningorg").html(""); 
     for(var i=0;i<jsonList.lenght;i++){ 

      listItems+="<option value='" + jsonList.owningorg[i].owning_org_id + "'>" + jsonList.owningorg[i].owning_org_dec[i] + "</option>"; 

      } 



     } 
    }); 

我的問題是如何讓owning_org_dec和Owning_org_id填充到價值和降使用jQuery的遞減。我目前在下拉菜單中獲得空值。請向我解釋如何從JSON對象和JSON數組中檢索數據到下拉列表或指向帖子或教程。感謝您的時間。

回答

0

類似:

var thisVal = jsonList.owningorg[i].owning_org_id; 
var thisTxt = jsonList.owningorg[i].owning_org_dec[i]; 
$("<option />").attr("value", yourvalue).text(thisTxt).appendTo("#NAMEofSelect"); 

記住,你必須通過你要appendTo選擇字段的名稱。 (#NameofSelect)

+0

當我試圖此螢火是顯示以下錯誤----類型錯誤我:jsonList.owningorg未定義 [打破該誤差] \t 變種thisVal = jsonList.owningorg [1] .owning_org_id; – VBJ 2013-02-25 21:49:40

相關問題