检索模板中的 json 值

发布于 2024-10-30 22:16:43 字数 1264 浏览 1 评论 0原文

我从一个表中查询了以下对象,然后将各种对象放入以下 ​​objarr 中。如何在 javascript

 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 中的 UI 中检索这些值给出以下内容

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尽揽少女心 2024-11-06 22:16:43

您还可以尝试下面的代码来实现该功能:

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

希望这对您有用..:)

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.. :)

动次打次papapa 2024-11-06 22:16:43

您需要下载 json2.js 文件 并将其添加到您的应用程序

中 在响应中尝试此

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

并将它们分配到变量中...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文