开发者

Retrieve json value in templates

I have the following objects queried form a table after which the various objects are put In the following objarr.How to retrieve these values in UI in javascript

 fr开发者_运维百科om 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 gives the following

   {'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, }}] }

In the UI i try to have to access the value of emp_id ,how do i do it

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


You can also try below code to make that work:

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

Hope this works for you.. :)


You need to download the json2.js file and add it to your application

In the response try this

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
  }
}

You will get the value of emp_id in the variable emp..

Similarly do for others

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

and assigning them in a variable...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜