如何使用json.parse解析json不断收到错误 [英] How to parse json using json.parse keep getting error

查看:175
本文介绍了如何使用json.parse解析json不断收到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用json.parse(rawdata)解析json数组;我还没有打印出控制台日志。



另外,showstring方法不起作用或者不确定原因,它甚至不会在控制台中抛出任何错误。



对于一个有效的showxmlfilms,但显示整个表格,我想在用户输入id时只显示一部电影而不是所有电影。



注意Erro是144行。



我尝试过:



我创建了json数组并使用foor循环发送它,但每次都会出现相同的错误。



错误打开,JSON.parse(rawData);





I am trying to parse json array using json.parse(rawdata); I have printed out console logs still nothing.

Also, the showstring method isn't working either not sure why, it's not even throwing nay errors in the console.

For the showxmlfilms that one works, but shows the entire table and I would like to show just one film instead of all the films, when user inputs id.

Note Erro is n line 144.

What I have tried:

I have created json array and sending that using foor loop, but still comes up with the same error each time.

Error is on, JSON.parse(rawData);


//Creating function to get the films table
function getFilmsTable(rows) {
	// Creating the different headings for the table
	var headings = 
		[ "id", "title", "year", "director", "stars", "review" ];
	// Return the table with the headings and rows
	return(getTable(headings, rows));
} // CLose function get films table

//////////////////////////////////XML Format /////////////////////////////////
//Creating function for xmlFilmsTableresult
function xmlFilmsTableresult(resultRegion, field1, field2) {
	// Creating address for show films
	var address = "show-films";
	// Creating var data for make param string
	var data = makeParamString(field1, field2, "xml");
	// Creating ajax post for the address data and function request
	// The function request will then showXmlFilmsInfo
	ajaxPost(address, data, 
			function(request) {
		showXmlFilmsInfo(request, resultRegion); 
	}); // Close ajax post and function request
} // Close function xmlFilmsTableresult



//Creating docuemnt.ready function for the films in xml format
//Calling btnXmlFilmsTable and creating jquery
$(document).ready(function() {
	$("#btnXmlFilmsTable").click(function() {
		// Creating url to convert the film to xml format
		var address = "http://localhost:8080/Control?format=xml";
		// Client = 123
		// Crating var data and setting it to empty string
		var data = "";
		// Crating ajax and calling the url, and calling the showXmlFilmsInfo function
		$.ajax({
			url: address,
			success:  function(data) {
				showXmlFilmsInfo(data);
			} // Close function data
		}); // close ajax
	}); // Close .click function for the jquery
}); // Close document.ready function for the jquery


//Creating function for the showXmlFilmsInfo
function showXmlFilmsInfo(request) {
	// Creating if statement for request not equal to null
	if (request!=null) {
		// Creating var for xmlDocument equal to request 
		var xmlDocument = request;
		// Getting the eleement by tag name (film)
		var films = xmlDocument.getElementsByTagName("film");
		// Creating var for rows and new array
		var rows = new Array();
		// Creating for loop for the films length
		for(var i=0; i<films.length; i++) {
			var film = films[i];
			// Creating var for the sub elements
			var subElements = [ "id", "title", "year", "director", "stars", "review" ];
			// Get element values
			rows[i] = getElementValues(film, subElements);
		} // Close for loop
		// Creating var table and getting the films table
		var table = getFilmsTable(rows);
		// Calling the xml films table
		$("#xml-films-table").html(table);
	} // Close if statement for request not equal to null
} // Close function showXmlFilmsInfo

////////////////////////////////////////json format /////////////////////////////////////////////
//function jsonFilmsTableresult(resultRegion, field1, field2) {
//var address = "show-films";
//var data = makeParamString(field1, field2, "json");
//ajaxPost(address, data, 
//function(request) { 
//showJsonFilmsInfo(request, resultRegion); 
//});
//}

//function showJsonFilmsInfo(request, resultRegion) {
//if (data!=null) {
//var rawData = request;
//var films = eval("(" + rawData + ")");
//var rows = new Array();
//for(var i=0; i<films.length; i++) {
//var Film = films[i];
//rows[i] = [Film.id, Film.title,
//Film.year, Film.director, Film.stars, Film.review];
//}
//var table = getFilmsTable(rows);
//htmlInsert(resultRegion, table);
//}
//}

//Creating function for jsonFilmsTableresult
function jsonFilmsTableresult(resultRegion, field1, field2) {
	// Creating address for show films
	var address = "show-films";
	// Creating var data for make param string
	var data = makeParamString(field1, field2, "json");
	// Creating ajax post for the address data and function request
	// The function request will then showJsonFilmsInfo
	ajaxPost(address, data, 
			function(request) {
		showJsonFilmsInfo(request, resultRegion); 
	}); // Close ajax post and function request
} // Close function jsonFilmsTableresult



//Creating docuemnt.ready function for the films in json format
//Calling btnJsonFilmsTable and creating jquery
$(document).ready(function() {
	$("#btnJsonFilmsTable").click(function() {
		// Creating url to convert the film to json format
		var address = "http://localhost:8080/Control?format=json";

		// Crating var data and setting it to empty string
		var data = "";
		// Crating ajax and calling the url, and calling the showJsonFilmsInfo function
		$.ajax({
			url: address,
			success:  function(data) {
				showJsonFilmsInfo(data);
			} // Close function data
		}); // close ajax
	}); // Close .click function for the jquery
}); // Close document.ready function for the jquery


//Creating function for the showJsonFilmsInfo
function showJsonFilmsInfo(request) {
	// Creating if statement for request not equal to null
	if (request!=null) {
//		console.log(request);
//		alert(request);
		var rawData = request;
		console.log(rawData);
//		var films = eval("(" + rawData + ")");
		// parsing the jason data for the films
//		var films = JSON.parse("(" + rawData + ")");
		JSON.parse(rawData);
		console.log(rawData);
		
		// Creating var for rows and new array
		var rows = new Array();
		// Creating for loop for the films length
		for(var i=0; i<rawData.length; i++) {
			var rawData = rawData[i];
			// Creating var for the sub elements
			var subElements = [ "id", "title", "year", "director", "stars", "review" ];
			// Get element values
			rows[i] = getElementValues(rawData, subElements);
		} // Close for loop
		
//		var json = "{\"name\": \"Dhanyaal Rashid\"}"
//		alert(JSON.parse(json).name);

		// Creating var for rows and new array
		var rows = new Array();
		// Creating for loop for the films length
		for(var i=0; i<films.length; i++) {
			var film = films[i];
			// Creating var for the sub elements
			var subElements = [ "id", "title", "year", "director", "stars", "review" ];
			// Get element values
			rows[i] = getElementValues(film, subElements);
		} // Close for loop
		// Creating var table and getting the films table
		var table = getFilmsTable(rows);
		// Calling the json films table
		$("#json-films-table").html(table);
	} // Close if statement for request not equal to null
} // Close function showXmlFilmsInfo



///////////////////////////////////////// String format //////////////////////////
//function stringFilmsTableresult(resultRegion, field1, field2) {
//var address = "show-films";
//var data = makeParamString(field1, field2, "string");
//ajaxPost(address, data, 
//function(request) { 
//showStringFilmsInfo(request, resultRegion); 
//});
//}

//(document).ready(function() {			
//$("#stringFilmsTable").click(function() {
//$("#stringFilmsTable").html("Data in string format");

//var address = "show-films";
//var data = makeParamString(field1, field2, "string");
//ajaxPost(address, data, 
//function(request) { 
//showStringFilmsInfo(request, resultRegion);
//});
//});
//});



//function showStringFilmsInfo(request, resultRegion) {
//if ((request.readyState == 4) &&
//(request.status == 200)) {
//var rawData = request.responseText;
//var film = rawData.split(/\n+/);
//var rows = new Array();
//for(var i=0; i<films.length; i++) {
//if (films[i].length > 1) {  // Ignore blank lines
//rows.push(films[i].split("#"));
//}
//}
//var table = getFilmsTable(rows);
//htmlInsert(resultRegion, table);
//}
//}


//Creating function for stringFilmsTableresult
function stringFilmsTableresult(resultRegion, field1, field2) {
	// Creating address for show films
	var address = "show-films";
	// Creating var data for make param string
	var data = makeParamString(field1, field2, "string");
	// Creating ajax post for the address data and function request
	// The function request will then showStringFilmsInfo
	ajaxPost(address, data, 
			function(request) {
		showStringFilmsInfo(request, resultRegion); 
	}); // Close ajax post and function request
} // Close function stringFilmsTableresult



//Creating docuemnt.ready function for the films in string format
//Calling btnStringFilmsTable and creating jquery
$(document).ready(function() {
	$("#btnStringFilmsTable").click(function() {
		// Creating url to convert the film to string/plain text format
		var address = "http://localhost:8080/Control?format=string";
		// Crating var data and setting it to empty string
		var data = "";
		// Crating ajax and calling the url, and calling the showStringFilmsInfo function
		$.ajax({
			url: address,
			success:  function(data) {
				showStringFilmsInfo(data);
			} // Close function data
		}); // close ajax
	}); // Close .click function for the jquery
}); // Close document.ready function for the jquery


//Creating function for the showStringFilmsInfo
function showStringFilmsInfo(request) {
	if ((request.readyState == 4) &&
			(request.status == 200)) {
		var rawData = request.responseText;
		var film = rawData.split(/\n+/);
		var rows = new Array();
		for(var i=0; i<films.length; i++) {
			if (films[i].length > 1) {  // Ignore blank lines
				rows.push(films[i].split("#"));
			}
		}


		// Creating var table and getting the films table
		var table = getFilmsTable(rows);
		// Calling the string films table
		$("#string-films-table").html(table);
	} // Close if statement for request not equal to null
} // CLose foor loop show string


////////////////////////////////////////////Creating the table with the different colmns //////////////////
[ "id", "title", "year", "director", "stars", "review" ];

function filmsTable(filmTypeField, formatField, resultRegion) {
	var address = "show-films";
	var filmid = getValue(filmTypeField);
	var filmtitle = getValue(filmTypeField);
	var filmyear = getValue(filmTypeField);
	var filmdirector = getValue(filmTypeField);
	var filmstars = getValue(filmTypeField);
	var filmreview = getValue(filmTypeField);
	var format = getValue(formatField);
	var data = "filmid=" + filmid +
	"filmtitle=" + filmtitle +
	"filmyear=" + filmyear +
	"filmdirector=" + filmdirector +
	"filmstars=" + filmstars +
	"filmreview=" + filmreview +
	"&format=" + format;
	var responseHandler = findHandler(format);
	ajaxPost(address, data, 
			function(request) { 
		responseHandler(request, resultRegion); 
	});
}


//Reminder: unlike in Java, in JavaScript it is OK to 
//use == to compare strings.

function findHandler(format) {
	if (format == "xml") {
		return(showXmlFilmsInfo);
	} else if (format == "json") {
		return(showJsonFilmsInfo);
	} else {
		return(showStringFilmsInfo);
	}
}

推荐答案

(文件).ready(function(){
(document).ready(function() {


(#btnXmlFilmsTable)。click(function(){
//创建url将电影转换为xml格式
var address =http:// localhost:8080 / Control ?format = xml;
// Client = 123
// Crating var data并将其设置为空字符串
var data =;
// Crating ajax并调用url,并调用showXmlFilmsInfo函数
("#btnXmlFilmsTable").click(function() { // Creating url to convert the film to xml format var address = "http://localhost:8080/Control?format=xml"; // Client = 123 // Crating var data and setting it to empty string var data = ""; // Crating ajax and calling the url, and calling the showXmlFilmsInfo function


.ajax({
url:address,
success:function(data){
showXmlFilmsInfo(data) ;
} //关闭函数数据
}); //关闭ajax
}); //关闭jquery
的.click函数}); //关闭jquery的document.ready函数


//为showXmlFilmsInfo创建函数
函数showXmlFilmsInfo(request){
//为请求创建if语句不等于null
if(request!= null){
//为xmlDocument创建var等于request
var xmlDocument = request;
//按标签名称获取元素(电影)
var films = xmlDocument.getElementsByTagName(film);
//为行和新数组创建var
var rows = new Array();
//为电影制作循环长度
for(var i = 0; i< films.length; i ++){
var film = films [i];
//为子元素创建var
var subElements = [id,title,year,director,stars,review];
//获取元素值
rows [i] = getElementValues(film,subElements);
} //关闭循环
//创建var表并获取电影表
var table = getFilmsTable(rows);
//调用xml电影表
.ajax({ url: address, success: function(data) { showXmlFilmsInfo(data); } // Close function data }); // close ajax }); // Close .click function for the jquery }); // Close document.ready function for the jquery //Creating function for the showXmlFilmsInfo function showXmlFilmsInfo(request) { // Creating if statement for request not equal to null if (request!=null) { // Creating var for xmlDocument equal to request var xmlDocument = request; // Getting the eleement by tag name (film) var films = xmlDocument.getElementsByTagName("film"); // Creating var for rows and new array var rows = new Array(); // Creating for loop for the films length for(var i=0; i<films.length; i++) { var film = films[i]; // Creating var for the sub elements var subElements = [ "id", "title", "year", "director", "stars", "review" ]; // Get element values rows[i] = getElementValues(film, subElements); } // Close for loop // Creating var table and getting the films table var table = getFilmsTable(rows); // Calling the xml films table


这篇关于如何使用json.parse解析json不断收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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