473,657 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert a table to JSON (or to Java array)

I'm using Prototype.js and would like to convert the contents of an
HTML table to JSON. Converting to an array first is fine too. Any
thoughts on this? I haven't seen anyone do anything this....

thanks
phil

Jun 13 '07 #1
2 6863
On Jun 14, 9:19 am, "phil.swen...@g mail.com" <phil.swen...@g mail.com>
wrote:
I'm using Prototype.js
If you want help specifically with Prototype.js, ask on the Ruby on
Rails spinoffs group.

and would like to convert the contents of an
HTML table to JSON. Converting to an array first is fine too. Any
thoughts on this? I haven't seen anyone do anything this....
Sounds fairly trivial. If by "content" you mean text content, loop
over the rows and create one array for each, stuff the contents of
each cell into a row array element and return it as a JSON formatted
text string, e.g.

function table2JSON (t)
{
function getText(el){
if (typeof el.textContent == 'string')
return el.textContent;
if (typeof el.innerText == 'string')
return el.innerText;
return '';
}

var rows = t.rows;
var cells;
var t, jsonText = [];

for (var i=0, nrows=rows.leng th; i<nrows; i++){
cells = rows[i].cells;
t = [];

for (var j=0,ncells=cell s.length; j<ncells; j++){
t.push(getText( cells[j]));
}

jsonText.push('[' + t.join(', ') + ']');
}

return '[' + jsonText.join(' , ') + ']';
}
--
Rob
Jun 14 '07 #2
RobG wrote:

Hi,
Sounds fairly trivial.
If by "content" you mean text content, loop
over the rows and create one array for each, stuff the contents of
each cell into a row array element and return it as a JSON formatted
text string, e.g.
Your code does convert the table content into arrays (which was the
first requirement, indeed trivial), but you seem to have stopped before
converting to the JSON format :)

<URL:http://www.json.org/>

AFAICS, a JSON entity can contain values such as string, number, object,
array, true, false, null. Since we would originally be parsing text, I
would say that objects and arrays should probably be left unexpanded,
however other values should probably be analyzed and JSON-formatted,
given their final status (valid value or text).

Values such as 'true', false' and 'null' are left "as is", numbers
should be trimmed and checked against a good regexp, and strings should
be surrounded by double quotes (taking care of escaping issues,
essential in serialization processes).
Regards,
Elegie.

Jun 14 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3380
by: werrt | last post by:
What is the best way to store the following table in JAVA Is there anyway to get values using column names? A B C D E F 1 25 54 55 77 14 5 2 2 43 11 25 10 31 3 5 55 4 8 5 54
1
4734
by: Eugene | last post by:
Hi All, I am having a hard time figuring out how to pass a ("true") java array, like int, from a jdbc application in to a DB2 stored procedure or UDF (can be a java or SQL proc or a table UDF). Having the application to write the array to a DB2 global temp table, or passing it in any surrogate form like delimited string (or a byte stream) of elements, are not possible alternatives. Is that implementable at all with DB2 jdbc? (I am using...
2
403
by: Michael Meckelein | last post by:
Hi all! I have a table like Table myTable = new Table(); Now, I want to publish the table as part of a webpage and as part of a word document. I tried to convert the table, but the compiler say "Cannot convert type 'System.Web.UI.WebControls.Table' to 'System.Web.UI.HtmlControls.HtmlTable'" if I use (HtmlTable)myTable.
3
18186
by: Mike | last post by:
Hi, Does anyone know of reliable programs that convert C# to Java and viceversa? Thanks Mike
4
36011
by: kvicky | last post by:
I have multiline textbox and I am trying to do a search functionality based on the input entered in this Textbox. I am able to convert into a string array if the search components are separated by commas, for ex (Image1, Image2) by using the StringArrayConverter class available in ..Net 2.0. But I also want to convert the strings entered in new line for example if I enter Image1 Image2
5
6345
by: Maxim | last post by:
Hi all, I'm calling a COM Interface method that returnes SafeArray wrapped into variant. Is it possible to convert it to managed array? Because working with SAFEARRAY directly is a bit complicated. Or maybe there is a managed wrapper class for safe array? Thanks in advance.
4
17347
by: Man4ish | last post by:
HI , I am trying to convert string into char array of characters.but facing problem. #include <iostream> #include <string> using namespace std; int main() { string t="1,2,3,4,5,6";
6
1250
by: =?Utf-8?B?Q2hhcmxlcw==?= | last post by:
Please help me convert the following Java to C# class ProgramChoice { private Class cls; private String text; ProgramChoice(String text, Class cls) { this.cls = cls; this.text = text; }
12
13537
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} System.Text.UTF8Encoding str = new System.Text.UTF8Encoding(); string s = new string((char)107, 1); s += new string((char)62, 1);
2
3750
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error when i do the following. if you add a contact with up to 13 fields it will be stored in a data structure. i have a tabbed pane that will show six of the 13 fields for that contact. when you double click the contact i want it to pop up and show all 13...
0
8395
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.