json tables problem

Talk about whatever topic you'd like, RPG related or not. (But please discuss things related to our software in the Tools section, below.)

Moderators: dorpond, trevor, Azhrei

Post Reply
wisewolf
Cave Troll
Posts: 79
Joined: Tue Aug 06, 2019 10:50 am

json tables problem

Post by wisewolf »

My second question :o

In the Runequest campaign I have a problem when showing json tables. I am really new to this so I can't see where is the problem. For example, with wepons, the json looks this way (I only copy a single weapon so it is not too long):

Code: Select all

[{"Nom":"Alabarda","Mains":"2H","Competence":"Estilo: Close Combat","Category":"Close Combat","Degats":"1D8+2","Type":"Yes","Portee":2.5,"Attaques par Round":"1","Recharge":"0","Munitions":"0","Enrayement":"0","Bonus/Malus":"0","Epoque":"Runequest","Notes":"Can be set against a charge  ","Manoeuvres":"Atrapar","STR":7,"DEX":7,"Taille":"G","Allonge":"ML","PA":4,"PV":10,"Perce-Armure":"0","Description":"An axe head combined with a thrusting tip and a hook, mounted on a spear shaft, the halberd is a multi-purpose weapon. Halberdiers are versatile opponents that can bring any version of the weapon&#39s attack to bear in an instant. ","Prix":"250","Monnaie":"SP","Poids":"4"},
I have attached a screenshot to see how it is shown.


Here is the macro call:

Code: Select all

[h:AllWeapons=getLibProperty("AllWeapons","Lib:gm")]

[frame("Weapons","temporary=0"):
{
	[macro("JsonToTable@lib:gm"):AllWeapons]					
}]
And here is the macro:

Code: Select all

[h:jsonarray=Macro.args]
[h:jsonarraylength=json.length(jsonarray)]
[h:jsonobject=json.get(jsonarray,0)]
[h:jsonobjectfields = json.fields(jsonobject)]
[h:jsonobjectattrib = json.get(jsonobject,"Nom")]
[h:jsonobjectlength=json.length(jsonobject)]

[h:numberOfRows = jsonarraylength]
[h:numberOfColumns = jsonobjectlength]

[h: oddRow = '#FFFFFF']
[h: evenRow = '#EEEEAA']

<html>
<head>
<title>Tableau</title>
<link rel="stylesheet" type="text/css" href="generic_css@Lib:gm"></link>
</head>
<body>
<table border="0" CELLPADDING=5 Align=center width=100%>
	[for(j,0,NumberOfColumns,1,""):"<th Align=center>"+ listGet(jsonobjectfields,j)]</th>
	[h:bgColor = oddRow]
	[for(i,0,NumberOfRows,1,""),code:
		{

		[h:jsonobject=json.get(jsonarray,i)]
		[if ((i/10)==floor(i/10) && i!=0),CODE :
		{ 
		[for(j,0,NumberOfColumns,1,""):"<th Align=center>"+ listGet(jsonobjectfields,j)]</th></tr>
		};{}]
		<tr>
		[for(j, 0, NumberOfColumns, 1, ""), code:
			{
			<td align='center' bgcolor=[r:bgColor]><span color='black'><b>
			[h:jsonobjectattrib=listGet(jsonobjectfields,j)]
			[json.get(jsonobject,jsonobjectattrib)]	
			</td>
			}]
		</tr>
		[h:bgColor = if(bgColor == oddRow, evenRow, oddRow)]	
		}]	
</table>
 </body>
 </html>
Thank you very much in advance.
Attachments
Sin título.jpg
Sin título.jpg (635.18 KiB) Viewed 1049 times
Last edited by wolph42 on Wed Dec 11, 2019 4:16 am, edited 1 time in total.
Reason: replaced quote for code tags


wisewolf
Cave Troll
Posts: 79
Joined: Tue Aug 06, 2019 10:50 am

Re: json tables problem

Post by wisewolf »

As you maybe can see on the attached screenshot, each column is shown with all the code inside every cell, where it should be shown just the "name", "damage", "reach", etc.

For example, in Category column, this is what is shown:
« json.get(jsonobject,jsonobjectattrib) = json.get({"Nom":"Alabarda","Mains":"2H","Competence":"Estilo: Close Combat","Category":"Close Combat","Degats":"1D8+2","Type":"Yes","Portee":2.5,"Attaques par Round":"1","Recharge":"0","Munitions":"0","Enrayement":"0","Bonus/Malus":"0","Epoque":"Runequest","Notes":"Can be set against a charge ","Manoeuvres":"Atrapar","STR":7,"DEX":7,"Taille":"G","Allonge":"ML","PA":4,"PV":10,"Perce-Armure":"0","Description":"An axe head combined with a thrusting tip and a hook, mounted on a spear shaft, the halberd is a multi-purpose weapon. Halberdiers are versatile opponents that can bring any version of the weapon's attack to bear in an instant. ","Prix":"250","Monnaie":"SP","Poids":"4"}, Epoque) = Runequest »
I didn't write the code so I don't know exactly what the problem is, and I am not used to json tables :roll:

It seems like it shows the complete json instead of showing just the category.

User avatar
metatheurgist
Dragon
Posts: 364
Joined: Mon Oct 26, 2009 5:51 am

Re: json tables problem

Post by metatheurgist »

For starters it looks like a lot of code has been replaced by html entities. I can't believe that works at all.

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: json tables problem

Post by wolph42 »

I did see the screenshot and I could make an educated guess what the issue was/is, but it would be exactly that: a guess. Over the years I've started to refrain from going into posts where people completely forget to ask what they want...

I noticed the html markup as well and most likely if you fix that then 'it' will be fixed.

For starters I would suggest you copy paste the code into a text editor and search and replace:
"
for
"
So this:

Code: Select all

[h:jsonobjectattrib = json.get(jsonobject,"Nom")]
turns into this

Code: Select all

[h:jsonobjectattrib = json.get(jsonobject,"Nom")]
There are probably other markups as well (like '), so also do a search for & to check out the other and replace them as well. Then paste the code back and see what happens.

edit: another thing which might be quicker is to place the code in an html form, render it and then copy paste the result back. Not 100% sure though that this will go rigth (but it should).

wisewolf
Cave Troll
Posts: 79
Joined: Tue Aug 06, 2019 10:50 am

Re: json tables problem

Post by wisewolf »

Well it is not really that way, but when I open it with Notepad++ it shows those entities changed. Anyway inside Maptool the macros are shown with the correct entities, so it can't be the problem here.

I attach here the macro shown inside Maptool
Attachments
Sin título.jpg
Sin título.jpg (192.13 KiB) Viewed 1032 times

wisewolf
Cave Troll
Posts: 79
Joined: Tue Aug 06, 2019 10:50 am

Re: json tables problem

Post by wisewolf »

And here in plain text

Code: Select all

[h:jsonarray=Macro.args]
[h:jsonarraylength=json.length(jsonarray)]
[h:jsonobject=json.get(jsonarray,0)]
[h:jsonobjectfields = json.fields(jsonobject)]
[h:jsonobjectattrib = json.get(jsonobject,"Nom")]
[h:jsonobjectlength=json.length(jsonobject)]

[h:numberOfRows = jsonarraylength]
[r:numberOfRows]
[h:numberOfColumns = jsonobjectlength]
[r:numberOfColumn]

[h: oddRow = '#FFFFFF']
[h: evenRow = '#EEEEAA']

<html>
<head>
<title>Tableau</title>
<link rel="stylesheet" type="text/css" href="generic_css@Lib:gm"></link>
</head>
<body>
<table border="0" CELLPADDING=5 Align=center width=100%>
	[for(j,0,NumberOfColumns,1,""):"<th Align=center>"+ listGet(jsonobjectfields,j)]</th>
	[h:bgColor = oddRow]
	[for(i,0,NumberOfRows,1,""),code:
		{

		[h:jsonobject=json.get(jsonarray,i)]
		[if ((i/10)==floor(i/10) && i!=0),CODE :
		{ 
		[for(j,0,NumberOfColumns,1,""):"<th Align=center>"+ listGet(jsonobjectfields,j)]</th></tr>
		};{}]
		<tr>
		[for(j, 0, NumberOfColumns, 1, ""), code:
			{
			<td align='center' bgcolor=[r:bgColor]><span color='black'><b>
			[h:jsonobjectattrib=listGet(jsonobjectfields,j)]
			[json.get(jsonobject,jsonobjectattrib)]	
			</td>
			}]
		</tr>
		[h:bgColor = if(bgColor == oddRow, evenRow, oddRow)]	
		}]	
</table>
 </body>
 </html>

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: json tables problem

Post by aliasmask »

I would check your preferences and make sure these settings are checked as I have them. I also recommend putting your output in to a variables and then outputting in the end.
Attachments
MT Preferences.jpg
MT Preferences.jpg (125.17 KiB) Viewed 1028 times

Merudo
Giant
Posts: 228
Joined: Wed Jun 05, 2019 7:06 am

Re: json tables problem

Post by Merudo »

I translated this macro to English and fixed the display bug when I worked on my Call of Cthulhu framework.

Here is my version. It's working pretty good:

Code: Select all

[h:jsonarray=Macro.args]
[h:jsonarraylength=json.length(jsonarray)]
[h:jsonobject=json.get(jsonarray,0)]
[h:jsonobjectfields = json.fields(jsonobject)]
[h:jsonobjectattrib = json.get(jsonobject,"Name")]
[h:jsonobjectlength=json.length(jsonobject)]

[h:numberOfRows = jsonarraylength]
[h:numberOfColumns = jsonobjectlength]

[h: oddRow = '#FFFFFF']
[h: evenRow = '#EEEEAA']

<html>
<head>
<title>Table</title>
<link rel="stylesheet" type="text/css" href="generic_css@Lib:gm"></link>
</head>
<body>
<table border="0" CELLPADDING=5 Align=center width=100%>
	[r, for(j,0,NumberOfColumns,1,""):"<th Align=center>"+ listGet(jsonobjectfields,j)]</th>
	[h:bgColor = oddRow]
	[r, for(i,0,NumberOfRows,1,""),code:
		{

		[h:jsonobject=json.get(jsonarray,i)]
		[if ((i/10)==floor(i/10) && i!=0),CODE :
		{ 
		[r, for(j,0,NumberOfColumns,1,""):"<th Align=center>"+ listGet(jsonobjectfields,j)]</th></tr>
		};{}]
		<tr>
		[r, for(j, 0, NumberOfColumns, 1, ""), code:
			{
			<td align='center' bgcolor=[r:bgColor]><span color='black'><b>
			[h:jsonobjectattrib=listGet(jsonobjectfields,j)]
			[r: json.get(jsonobject,jsonobjectattrib)]	
			</td>
			}]
		</tr>
		[h:bgColor = if(bgColor == oddRow, evenRow, oddRow)]	
		}]	
</table>
 </body>
 </html>
I think the big change is that I put a "r, " in the "for" loop statements.

EDIT: aliasmask posted a different solution, however I still prefer adding "r" (suppresses the quotes marks) because it will fix the issue for anyone using the macro, not just you.

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: json tables problem

Post by aliasmask »

Can you show us what your data looks like using this line of code at the top.

Code: Select all

 [dialog("D"):{<pre>[R: json.indent(replace(macro.args,"<","&lt;"))]</pre>}]
I was going to give you an example of how to put all your output in to a variable.

Post Reply

Return to “General Discussion”