问题描述
我正在尝试使用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);
}
}
推荐答案
这篇关于如何使用json.parse解析json不断收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!