0

我正在尝试这个很长一段时间在这里搜索不同的帖子..但无法了解如何从 JSON 对象或数组获取数据/值的概念

我正在使用 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>";

            } 



        }
    }); 

我的问题是如何使用 Jquery 将 owning_org_dec 和 Owning_org_id 填充到 Drop 的 Value 和 desc 中。我目前将空值放入下拉列表。请向我解释如何从 JSON 对象和 JSON 数组中检索数据到下拉列表或指向帖子或教程。感谢您的时间。

4

1 回答 1

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");

请记住,您必须传递要附加到的选择字段的名称。(#NameofSelect)

于 2013-02-25T21:44:48.347 回答