HTML Table to JSON

I have an html code with javascript inside it...

<!DOCTYPE html>
<html>
<head>
	<title>Table to JSON Example</title>
</head>
<body>
	<table id="myTable">
		<thead>
			<tr>
				<th>Name</th>
				<th>Age</th>
				<th>Gender</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>John</td>
				<td>25</td>
				<td>Male</td>
			</tr>
			<tr>
				<td>Jane</td>
				<td>30</td>
				<td>Female</td>
			</tr>
			<tr>
				<td>Bob</td>
				<td>40</td>
				<td>Male</td>
			</tr>
		</tbody>
	</table>

	<div id="output"></div>

	<script>
		function tableToJson(table) {
			var data = [];
			for (var i = 1; i < table.rows.length; i++) {
				var tableRow = table.rows[i];
				var rowData = {};
				for (var j = 0; j < tableRow.cells.length; j++) {
					var tableHeader = table.rows[0].cells[j].innerHTML;
					var tableData = tableRow.cells[j].innerHTML;
					rowData[tableHeader] = tableData;
				}
				data.push(rowData);
			}
			return data;
		}
		
		window.onload = function() {
			var table = document.getElementById("myTable");
			var json = tableToJson(table);
			console.log(json);
			var outputElement = document.getElementById("output");
			outputElement.innerHTML = JSON.stringify(json);
		};
	</script>
</body>
</html>

and the output of javascript is this

[  {    "Name": "John",    "Age": "25",    "Gender": "Male"  },  {    "Name": "Jane",    "Age": "30",    "Gender": "Female"  },  {    "Name": "Bob",    "Age": "40",    "Gender": "Male"  }]

and don't know how to run it in the app and get the result of javascript in a label..

please guide me anyone pleae

the js:

window.AppInventor.setWebViewString(JSON.stringify(tableToJson(document.getElementById("myTable"))))

2 Likes

can I load html instead of going to URL

the photo is loading from assets.
There is no block for loading html in Webviewer. but you can save the html in ASD, and load the file by "GoToUrl"

1 Like

what if i load html direct or goto link

answered.

1 Like

i m laoding webview by url but not working

Please read the community for how to ask a good question.

Only this info, no body can help you.
Show your relevant block here.

1 Like

Looks fine.
Is the Webviewer showing the table correctly?
Is the js string correctly copied?

1 Like

yes both are fine but no result

what's your url?

1 Like

Kerala State Lottery Result Today - Lottery Sambad

Sorry you are scraping data from a website.
Do you have permission for do this from this website?
Otherwise it's against community policy.

1 Like

i m just testing and trying javascript

i can use my website too but i dont have time to make a new html tables page

here is my website

Test (hellotestingmyblog.blogspot.com)

You already have the solution. Try to understand how it works.

1 Like

im not getting the result in label

It works for the sample you supplied.

1 Like