检索模板中的json值

时间:2011-04-05 10:23:13

标签: javascript jquery python json

我从表中查询了以下对象,之后将各种对象放在下面的objarr中。如何在javascript中在UI中检索这些值

 from django.core.serializers import serialize
 json = serialize("json", objarr)
 logging.debug(type(json))
 response_dict.update({'objarr' : (json) })

 return HttpResponse(simplejson.dumps(response_dict), mimetype = 'application/javascript')

Logging.debug提供以下内容

   {'obj_arr': '[{"pk": 56, "model": "upload_info", "fields": {"emp_id": 13, "import_flag": 1, "resource": null, "feedback": "some feedabck", "hint": "test", "time": null, "created_by": 145, "access": 0, "keywords": "test1,test9", "type": 4, "error_flag": 0, }}, {"pk": 1156, "model": "upload_info", "fields": {"emp_id": 13, "import_flag": 1, "resource": null, "feedback": "some feedabck", "hint": "test", "time": null, "created_by": 145, "access": 0, "keywords": "test1,test9", "type": 4, "error_flag": 0, }}] }

在UI中我尝试访问emp_id的值,我该怎么做

function retrieve_data(formid)
{
  var form = $(formid);
  form.ajaxSubmit({
  dataType:  'json',
  success:   function (data) {  //Data is the rendered oject of resposne_dict
  if((data)
  {
     alert(load_flag);
     How to print emp_id,error_flag and other details here
  }
  }
 } )   ;
}

2 个答案:

答案 0 :(得分:1)

您还可以尝试以下代码来完成这项工作:

String.prototype.toArr = function() {
    eval("var obj = " + this);
    return obj ? obj : [];
};
function retrieve_data(formid){
  var form = $(formid);
  form.ajaxSubmit({
   dataType: 'json',
   success: function (data) {
   if(data){
     data = data.toArr();
     alert(load_flag);
     alert(data['fields']["emp_id"]);
     // And likewise you can access all detail
   }
  }
 });
}

希望这对你有用.. :))

答案 1 :(得分:0)

您需要下载json2.js file并将其添加到您的应用

在回复中试试这个

if((data)
{
  var response=eval("("+JSON.stringify(data)+")");
  for(var i=0;i<esponse.obj_arr[0].fields;i++){                        
    var emp=  response.obj_arr[0].fields[i].emp_id
  }
}

您将在变量emp中获得emp_id的值。

同样为他人做

response.obj_arr[0].fields[i].import_flag  
response.obj_arr[0].fields[i].resource
response.obj_arr[0].fields[i].feedback

并将它们分配给变量......