【问题标题】:Parse more json in the same html page with a jquery function使用 jquery 函数在同一个 html 页面中解析更多 json
【发布时间】:2014-04-29 09:40:02
【问题描述】:

您好,我的 Web 应用程序有问题。在一个 html 页面中,我有两个以上的 json(这是 php 页面的结果),并且使用 jquery 函数我必须解析它们。 问题是,如果页面中只有一个用于解析的 json,则没有问题,但如果有两个或更多 json,则它不起作用。哪个是问题? 有人可以帮助我吗? 这是 json 的两个例子:

{"Nr SAT":"SAT000000002574","Tipo Servizio":"GR","Stato":"ciao","Attributo":"APERTURA SAT SU IMEI DUPLICATO","Marca":"LG","Modello":"U830","Modello Guasto":"353142010979931"}
        {"Nr SAT":"SAT000000002574","Tipo Servizio":"Vedi anche me","Stato":"ahhhhhhhhhhhhh","Attributo":"APERTURA SAT SU IMEI DUPLICATO","Marca":"LG","Modello":"U830","Modello Guasto":"353142010979931"}

这些是我用于解析的 jquery 函数:

<script>

        $.getJSON('http://sath3g.altervista.org/index.php', { get_param: 'value' }, function(data) { 
            $('body').append($('<p>').html('Tipo di Servizio: '+ data["Tipo Servizio"]));
        });
        $.getJSON('http://sath3g.altervista.org/index.php', { get_param: 'value' }, function(data) { 
            $('body').append($('<p>').html('Stato: '+ data["Stato"]));
        });

        $.getJSON('http://sath3g.altervista.org/index.php', { get_param: 'value' }, function(data) { 
            $('body').append($('<p>').html('Nr SAT: '+ data["Nr SAT"]));
        });
        $.getJSON('http://sath3g.altervista.org/index.php', { get_param: 'value' }, function(data) { 
            $('body').append($('<p>').html('Attributo: '+ data["Attributo"]));
        });
        $.getJSON('http://sath3g.altervista.org/index.php', { get_param: 'value' }, function(data) { 
            $('body').append($('<p>').html('Marca: '+ data["Marca"]));
        });
    </script>

这是生成 json 的 php。 如果在控件“if ($line_of_text[0] == "SAT000000002574")”中我有多个结果,我可以通过哪种方式生成正确格式的 json?

<?php

  // Sopprimo gli errori del php
  error_reporting(0);

  // Includo la libreria
  require_once 'excel_reader2.php';

  //Recupero il valore del parametro "nome"

    $file_handle = fopen("leggimi2.csv", "r");

while (!feof($file_handle) ) {


$line_of_text = fgetcsv($file_handle, 1024);


    if ($line_of_text[0] == "SAT000000002574")
    {
        //print $line_of_text[0]." ".$line_of_text[1]." ".$line_of_text[2]." ".$line_of_text[3]." ".$line_of_text[4]." ".$line_of_text[5]." ".$line_of_text[6]." ".$line_of_text[7];


        $arr = array('Nr SAT' => $line_of_text[0], 'Tipo Servizio' => $line_of_text[1], 'Stato' => $line_of_text[2], 'Attributo' => $line_of_text[3], 'Marca' => $line_of_text[4], 'Modello' => $line_of_text[5], 
                'Modello Guasto' => $line_of_text[6]);


    echo json_encode($arr);

    }
 }

【问题讨论】:

    标签: jquery json html parsing


    【解决方案1】:

    你需要关注两件事:

    首先,在您的 JS 代码中,您正在对同一对象中的每个项目进行 ajax 调用。您多次请求相同的数据,这是不必要的。您应该将所有附加命令打包到一个 getJSON 调用中。

    其次,当您的 excel 文件中有多行时,您会一个接一个地回显多个对象,这会产生无效的 JSON。您应该将它们组织成一个 JSON 数组,并在您的 getJSON 回调中循环遍历它。

    代码如下:

    PHP

    <?php
    
    // Sopprimo gli errori del php
    error_reporting(0);
    
    // Includo la libreria
    require_once 'excel_reader2.php';
    
    //Recupero il valore del parametro "nome"
    
    $file_handle = fopen("leggimi2.csv", "r");
    
    //EDIT: Define an array to hold individual lines
    $data = array();
    
    while (!feof($file_handle) ) {
        $line_of_text = fgetcsv($file_handle, 1024);
        if ($line_of_text[0] == "SAT000000002574")
        {
            $arr = array('Nr SAT' => $line_of_text[0], 'Tipo Servizio' => $line_of_text[1], 'Stato' => $line_of_text[2], 'Attributo' => $line_of_text[3], 'Marca' => $line_of_text[4], 'Modello' => $line_of_text[5], 'Modello Guasto' => $line_of_text[6]);
    
            //EDIT: Add the line to the array
            $data[] = $arr;
        }
    }
    
    //EDIT: Encode the array as a JSON array of line objects
    echo json_encode($data);
    

    现在,返回到您的getJSON 调用的是一个对象数组。

    JS

    $.getJSON(
        'http://sath3g.altervista.org/index.php', { get_param: 'value' }, 
        function(data) {
            // 'data' is an array of objects here
            for(var i=0; i<data.length; i++) {
                //data[i] is an individual line of excel data
                $('body')
                    .append($('<p>').html('Tipo di Servizio: '+ data[i]["Tipo Servizio"]))
                    .append($('<p>').html('Stato: '+ data[i]["Stato"]))
                    .append($('<p>').html('Nr SAT: '+ data[i]["Nr SAT"]))
                    .append($('<p>').html('Attributo: '+ data[i]["Attributo"]))
                    .append($('<p>').html('Marca: '+ data[i]["Marca"]));
            }
        });
    

    【讨论】:

      【解决方案2】:

      JSON 似乎不正确。您应该返回如下 JSON 代码:

      [
        {"Nr SAT":"SAT000000002574",
         "Tipo Servizio":"GR",
          "Stato":"ciao",
         "Attributo":"APERTURA SAT SU IMEI DUPLICATO",
         "Marca":"LG","Modello":"U830", 
          "Modello Guasto":"353142010979931"
         },
         {"Nr SAT":"SAT000000002574",
         "Tipo Servizio":"Vedi anche me",
         "Stato":"ahhhhhhhhhhhhh",
         "Attributo":"APERTURA SAT SU IMEI DUPLICATO",
          "Marca":"LG", "Modello":"U830",
         "Modello Guasto":"353142010979931"
         }
      ]
      

      记得在数组中插入值。 你应该使用像http://jsonlint.com/这样的验证器

      PS: ciao, e speriamo che funzioni;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-28
        • 1970-01-01
        • 2022-01-22
        • 1970-01-01
        • 1970-01-01
        • 2016-02-17
        • 1970-01-01
        • 2020-06-10
        相关资源
        最近更新 更多