Click here to Skip to main content
15,888,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);
	}
}
Posted
Updated 11-Dec-18 14:36pm

1 solution

JSON still doesn't work for you Dhanyaal? Please do not take my statements harsh, or rude.

I recommend please visit JSON[^] website, and study the JSON notation for a while. Then, once you have done that, visit the JavaScript tutorials and please check them and try them for yourself.

It is almost impossible to help without knowing where things are going, and how you aim to develop this solution. It would be easier for you, to learn things and then try them. Here you say,
Quote:
Also, the showstring method isn't working either not sure why, it's not even throwing nay errors in the console.
then you say,
Quote:
Note Erro is n line 144.
Which statement to believe in? And which line is God forsaken 144th line in your code. It is harder to even inspect the code, let alone review and find the problem.

JavaScript | MDN[^]

I also recommend going through these links, they will help you in future,
Tips for asking technical questions that result in fast, useful solutions | Opensource.com[^]
How do I ask a good question? - Help Center - Stack Overflow[^]

To answer your question, if
JSON.parse(rawData);
fails, then try to do this,
JavaScript
alert(rawData); 
See what is in the variable, and verify it is a valid JSON. You can use something like an online parser, and verify it is okay (read the JSON notation to understand it yourself), if it is fine, your code would work fine. Otherwise, a safer way to write the code would be,
JavaScript
try {
   var obj = JSON.parse(rawData); // Parse it, in a safer fashion
} catch (error) {
   alert(error); // Show what went wrong
}
There are many other ways to safely work with JSON, but this is so far the easiest way to get around with these problems. Please go through my article as well, to learn a bit more on JSON notation and how different parsers work.

Json Parser Online[^]
 
Share this answer
 
Comments
Dhanyaal 11-Dec-18 19:51pm    
Thanks and if I add that try and catch in will that run now?
Afzaal Ahmad Zeeshan 12-Dec-18 4:53am    
Do you know what try...catch block does? I have given you a link, click on try or catch, it will take you to the documentation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900