我正在关注Arraylist
JSONArray JMyDataList= new JSONArray(MyDataList);
JSONArray JMyProjectData = new JSONArray(MyProjectData);
MyDataList:包含来自数据库的数据,列名为&数据
response.setContentType("application/json");
out = response.getWriter();
out.println(JMyDataList+"\n");
out.println(JMyProjectData );
out.close();
在javascript中,我能够看到响应对象,alert(xmlHttp.responseText);
CREATED_DATE
是MyDataList中的列名
var sampleJSON =xmlHttp.responseText;
var parsed_data = JSON.parse(sampleJSON);
alert(parsed_data.CREATED_DATE);
json对象
[{"CREATED_DATE":"1/12/2014","USER_ID":"DUMMY_SU"}]
[{"PROJ_NAME":"PROJ1"},{"PROJ_NAME":"PROJ2"},{"PROJ_NAME":"PROJ3"},{"PROJ_NAME":"PROJ4"}]
如何从此对象访问单个列值并分配到html页面上的文本框?
如何检索PROJ_NAME数组?
答案 0 :(得分:1)
parsed_data是一个对象数组。
<script>
for(var i=0; i<parsed_data.length; i++){
alert(parsed_data[i].CREATED_DATE);
}
</script>
2014年12月4日更新
我不确定你在服务器端有什么样的平台。
MVC部分:
public class Person{
public Id int {get; set;}
public Name string {get; set;}
}
public JsonResult MyActionName()
{
var object1 = new { Prop1="This is property 1", CREATED_DATE = DateTime.Today};
List<Person> persList = new List<Person>();
persList.Add(new Person(){Name = "Alex1", Id = 1});
persList.Add(new Person(){Name = "Alex2", Id = 2});
persList.Add(new Person(){Name = "Alex3", Id = 3});
persList.Add(new Person(){Name = "Alex4", Id = 4});
var result = new { _myObject = object1, _myList = persList};
return Json(result, JsonRequestBehavior.AllowGet);
}
Javascript部分: //使用jquery,因为它更容易
<script>
$.ajax({
url: "MyActionName",
dataType: "json",
success: function(data){
var object1 = data._myObject;
// object1 = { Prop1: "This is property 1", CREATED_DATE: "some date string"}
var _person;
for(var i=0; i<data._myList.length; i++){
_person = data._myList[i];
// _person = {Name: "Alex1", Id: 1}
// _person = {Name: "Alex2", Id: 2}
// _person = {Name: "Alex3", Id: 3}
// _person = {Name: "Alex4", Id: 4}
}
}
});
</script>