使用jquery函数在同一html页面中解析更多json
嗨,我有一个web应用程序的问题。在一个html页面中,我有两个以上的json(这是php页面的结果),我必须使用jquery函数解析它们。 问题是,如果页面中只有一个用于解析的json,那么就没有问题,但是如果有两个或更多的json,那么就不起作用。问题是什么? 有人能帮我吗? 以下是json的两个示例:使用jquery函数在同一html页面中解析更多json,jquery,json,html,parsing,Jquery,Json,Html,Parsing,嗨,我有一个web应用程序的问题。在一个html页面中,我有两个以上的json(这是php页面的结果),我必须使用jquery函数解析它们。 问题是,如果页面中只有一个用于解析的json,那么就没有问题,但是如果有两个或更多的json,那么就不起作用。问题是什么? 有人能帮我吗? 以下是json的两个示例: {"Nr SAT":"SAT000000002574","Tipo Servizio":"GR","Stato":"ciao","Attributo":"APERTURA SAT SU IM
{"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>
$.getJSON('http://sath3g.altervista.org/index.php“,{get_param:'value'},函数(数据){
$('body').append($('').html('Tipo di Servizio:'+data[“Tipo Servizio]”);
});
$.getJSON('http://sath3g.altervista.org/index.php“,{get_param:'value'},函数(数据){
$('body').append($('').html('Stato:'+data['Stato]]);
});
$.getJSON('http://sath3g.altervista.org/index.php“,{get_param:'value'},函数(数据){
$('body').append($('').html('Nr-SAT:'+data['Nr-SAT]]);
});
$.getJSON('http://sath3g.altervista.org/index.php“,{get_param:'value'},函数(数据){
$('body').append($('').html('attributeo:'+data['attributeo]]);
});
$.getJSON('http://sath3g.altervista.org/index.php“,{get_param:'value'},函数(数据){
$('body').append($('').html('Marca:'+data['Marca]]);
});
这是生成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);
}
}
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"
}
]
记住在数组中插入值。
你应该使用一个验证器,比如
PS:ciao,e speriamo che funzioni;) 您需要关注两件事: 首先,在JS代码中,对同一对象中的每个项进行ajax调用。您多次请求相同的数据,这是不必要的。您应该将所有append命令打包到一个
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(
'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"]));
}
});