使用JSON和多维数组(JS) [英] Working with JSON and a multidimensional array (JS)

查看:84
本文介绍了使用JSON和多维数组(JS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下问题:我编写了一个搜索函数,该函数将结果保存到数组中.当我使用 jquery表单插件处理该函数的响应时,我创建了一个附加数组由搜索创建的所有数组填充.然后,我想将该多数组作为JSON对象解析到我的jQuery脚本中.到目前为止一切顺利,但是我如何使多数组可访问脚本? (就像multiarray.array1.property一样)

I've got the following issue: I wrote a search function which results are being saved into an array. As i handle the response of that function with the jquery form plugin, i created an additional array which is filled with all of the arrays created by the search. Then, i want to parse that multi-array to my jQuery script as a JSON object. So far so good, but how do i make the multi-array accessable to the script? (Like multiarray.array1.property)

到目前为止,这是我的代码:

Here is my code so far:

  • [HTML/JS]
<!DOCTYPE html>
<html> 
<body>
<div class="edit">
<h2>Editieren</h2>
<form id="suchen" method="post"><input type="text" id="search" name="id">
<input type="submit" value="Senden"></form>  
</div>
</html>

    $('#suchen').ajaxForm({

    url: "./php/search.php",
    dataType: 'json',
    success: function(data){

        alert(data[0]) ;

    },
        clearForm: true
}) ;​

  • PHP
    • PHP
    • 提前谢谢

      JSON示例:

      {
          "id": "33",
          "firma": "sls",
          "strasse": "Industriegebiet Siebend",
          "plz": "66663",
          "ort": "Merzig",
          "vorname": "",
          "nachname": "Ruf",
              "email": "ronny.ruf@web.de ",
          "bemerkung": "",
          "partner": "",
          "kinder": "1",
          "nation": "D",
          "betreuer": "Adam",
          "anrede": "Herr"
      }
      

      推荐答案

      在您的PHP上

             while($row = mysql_fetch_array($result)){
      
                          $article = array (
      
                              "id"=>$row['id'],
                              "firma"=>$row['firma'],
                              "strasse"=>$row['strasse'],
                              "plz"=>$row['plz'],
                              "ort"=>$row['ort'],
                              "vorname"=>$row['vorname'],
                              "nachname"=>$row['nachname'],
                              "email"=>$row['email'],
                              "bemerkung"=>$row['bemerkung'],
                              "partner"=>$row['partner'],
                              "kinder"=>$row['kinder'],
                              "nation"=>$row['nation'],
                              "betreuer"=>$row['betreuer'],
                              "anrede"=>$row['anrede'],
      
                           ) ;
                          $hits[] = $article;
              }
      
                          echo json_encode($hits) ; 
      

      在您的jquery ...

      At your jquery...

      $('#suchen').ajaxForm({
      
          url: "./php/search.php",
          dataType: 'json',
          success: function(data){
             $.each(data, function(i, val){
                 console.log(val); /*alert don't work on arrays*/
                 alert(val.firma); /*you can alert some array key for example*/
             });    
          },
              clearForm: true
      }) ;​
      

      我用console.log替换了alert,因为val将是数组... 要访问其任何键,只需输入val.keyname ..

      I replace alert with console.log because val will be array... to access any of its keys just write val.keyname..

      为.. alert(val.strasse);

      这篇关于使用JSON和多维数组(JS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆