Hi the Im trying to get data from an api, I already deserialized the json,
But when I try to get the data in visualforce just shows me 1 value, and I dont get it, I ve been reading the documentation but still I have errors
Can anyone help me to understand what is wrong?
this is the response of my class
HTTPResponse response = h.send(request);
Map<String,object> results = new Map<String,object>();
List<Object> listResponse = new List<Object>();
Map<String,object> MapeoNombre = new Map<String,object>();
List<String> Nombre = new List<String>();
Integer counter = 0;
Integer num = 0;
Integer i = 1;
// Parse the JSON response
if(response.getStatusCode() == 200) {
results = (Map<String,object>)JSON.deserializeUntyped(response.getBody());
Map<String, Object> body = (Map<String, Object>)results.get('body');
listResponse = (List<Object>) body.get('asegurados');
num = listResponse.size();
System.debug('list:'+listResponse);
} else {
System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus());
}
for (Object asegurado:listResponse ){
if(counter<num){
MapeoNombre =(Map<String, Object>)asegurado;
Nombre.add((String)MapeoNombre.get('nombreAsegurado'));
i++;
}
itemName =(string)MapeoNombre.get('nombreAsegurado');
itemLastName =(string)MapeoNombre.get('apellidoPaterno');
itemSecondLastName =(string)MapeoNombre.get('apellidoMaterno');
itemRamo =(string)MapeoNombre.get('ramo');
itemPoliza =(string)MapeoNombre.get('poliza');
System.debug('nombre'+itemName);
System.debug('apellida paterno'+itemLastName);
System.debug('apellido materno'+itemSecondLastName);
System.debug('poliza'+itemPoliza);
}
and this is my visualforce
<apex:form >
<apex:inputText value="{!ramopoliza}"/>
<apex:pageBlock >
<apex:commandButton reRender="table1" value="Buscar" action="{!busqueda}"/>
</apex:pageBlock>
<apex:pageBlock id="table1">
<div class="painel">
<h1>MANCHA</h1>
<table>
<tr>
<th colspan="1" class="celula_em_branco"></th>
<th colspan="4" class="celula_azul_escuro">MANAGED SERVICES</th>
</tr>
<tr>
<td class="cor_cinza td_column">Nombre</td>
<td class="cor_cinza td_column">Apellido Paterno</td>
<td class="cor_cinza td_column">Apellido materno</td>
<td class="cor_cinza td_column">Ramo</td>
<td class="cor_cinza td_column">Poliza</td>
</tr>
<tr>
<td class="celula_azul_escuro">{!itemName}</td>
<td class="cor_azul">{!itemLastName}</td>
<td class="cor_azul">{!itemSecondLastName}</td>
<td class="cor_azul">{!itemRamo}</td>
<td class="cor_azul">{!itemPoliza}</td>
</tr>
<tr>
<td class="celula_azul_escuro">{!itemName}</td>
<td class="cor_azul">{!itemLastName}</td>
<td class="cor_azul">{!itemSecondLastName}</td>
<td class="cor_azul">{!itemRamo}</td>
<td class="cor_azul">{!miprueba}</td>
</tr>
</table>
</div>
<apex:pageblockTable value="{!Nombre}" var="f">
<apex:column value="{!f}"><apex:facet name="header">Object Names</apex:facet></apex:column>
</apex:pageblockTable>
</apex:form>
and this is the Json sample
"code": 1,
"message": "SUCCESS",
"date": "15/07/2022 23:57:00",
"body": {
"asegurados": [
{
"ramo": "37",
"poliza": "0000003101",
"endosoQueModifica": "000000",
"consecutivo": "00000001",
"iniciovigencia": "01/04/2022",
"finVigencia": "01/04/2023",
"nombreAsegurado": "MARCO ANTONIO",
"apellidoPaterno": "MARQUEZ",
"apellidoMaterno": "SOTELO",
"descripcionRamo": "SALUD INDIVIDUAL",
"dependiente": "01 TITULAR",
"fechaNacimiento": "14/08/1992",
"numCertificado": "00000001",
"numDeIncisoEnElCertificado": "00000001"
},
{
"ramo": "37",
"poliza": "0000003101",
"endosoQueModifica": "100002",
"consecutivo": "00000002",
"iniciovigencia": "20/04/2022",
"finVigencia": "01/04/2023",
"nombreAsegurado": "SAYRA FABIOLA",
"apellidoPaterno": "ORDUNO",
"apellidoMaterno": "ELIZONDO",
"descripcionRamo": "SALUD INDIVIDUAL",
"dependiente": "D DEPENDIENTE",
"fechaNacimiento": "22/10/1966",
"numCertificado": "00000001",
"numDeIncisoEnElCertificado": "00000002"
}
]
}
}
asegurados is a list
Related
I use datatable
A typical row is
<tr>
<td data-id="1">Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
I created an example
http://jsfiddle.net/hb7v1mgy/
Init of the table
var table = $('#example').DataTable({
responsive: true
});
When I click on a row, I would like to get data attritube id, actually I get column value (Tiger, System...)
$('#example tbody').on('click', 'tr', function() {
//get only value of td... not data attribute
var data = table.row(this).data();
});
In your click method
var tr = $(this).closest('tr');
var id = tr.children("td:eq(0)").attr('data-id')
you don't need select plugin...
<table>
<thead>
<th>name</th>
<th>place</th>
<th>area</th>
</thead>
<tbody>
<tr><td>name</td><td>jjhds</td><td>dfdsf</td></tr>
<tr><td>name</td><td>jjhds</td><td>dfdsf</td></tr>
<tr><td>name</td><td>jjhds</td><td>dfdsf</td></tr>
<tr><td>name</td><td>jjhds</td><td>dfdsf</td></tr>
<tr><td>name</td><td>jjhds</td><td>dfdsf</td></tr>
</tbody>
</table>
I have above table. I need to navigate from td in tbody and get its heading in thead****th through protractor. Please can anyone help?
One option would be to get the desired header by an index of a column in a row, sample:
var table = $('table'); // TODO: too general of a locator, improve
var headers = table.$$('th');
table.$$('tr').each(function (row) {
row.$$('td').each(function (cell, index) {
cell.getText().then(function (cellText) {
headers.get(index).getText().then(function (headerText) {
console.log("Header: " + headerText + ", Cell Value: " + cellText);
});
});
});
});
Using .each() here just for demonstration purposes.
I have the following code:
WebElement table = a_chromeWebDriver.findElement(By.className("stats-table"));
List<WebElement> allRows = table.findElements(By.tagName("tr"));
system.out.println(allRows.size());
for (WebElement row : allRows) {
ArrayList<WebElement> cells = (ArrayList<WebElement>) row.findElements(By.tagName("td"));
...
I am executing this code on a similar html like this one:
<table id="table-type-1" class="stats-table stats-main table-1">
<thead>
<tr class="main">
<th class="aaaa" data-type="rank" title="Rank">
</th>
A number of headers here...
</tr>
</thead>
<tbody title="">
<tr bla bla bla>
<td class="bla bla bla" title="bla bla bla>1.</td>
<td class="bla bla bla"></td>
<td class="bla bla bla">36</td>
<td class="bla bla bla">28</td>
<td class="bla bla bla">3</td>
</tr>
<td class="bla bla bla" title="bla bla bla>1.</td>
<td class="bla bla bla"></td>
<td class="bla bla bla">45</td>
<td class="bla bla bla">fnf</td>
<td class="bla bla bla">kfkfk</td>
</tr>
.
.
.
allRows size gives me a correct number of rows.
For some reason, I get cells = null.
I have also tried By.xpath("td") but with no use.
when you loop using this code :
for (WebElement row : allRows) {
ArrayList<WebElement> cells = (ArrayList<WebElement>) row.findElements(By.tagName("td"));
System.out.println("CellCount " + cells.size());
}
You will see that you get output as :
CellCount 0
CellCount 4
CellCount 4
That is because the first row that is picked up by your code is from <thead> section where there are no <td> elements rest it works fine; so you have to make exception for first <tr>
you can do that by changing your allRows element to following:
List<WebElement> allRows = table.findElements(By.xpath("tbody//tr"));
First, find all the rows and then iterate over them:
List<WebElement> allRows = driver.findElements(By.cssSelector("#table-type-1 > tbody > tr"));
for(WebElement row : allRows){
List<WebElement> cells = row.findElements(By.tagName("td"));
System.out.println("Cell count: " + cells.size());//cell count for a row
}
A HTML <table> consists of 3 sections: <thead>, <tbody> and <tfoot>.
Usually you will see only <thead> and <tbody>.
The correct way is to first identify each of these sections and then work your way through their child elements.
// Get the table
WebElement table = a_chromeWebDriver.findElement(By.id("table-type-1"));
// Get the thead in table
WebElement thead = table.findElement(By.tagName("thead"));
// Get the tbody in table
WebElement tbody = table.findElement(By.tagName("tbody"));
// Get all headers in thead
List<WebElement> allHeaders = thead.findElements(By.tagName("th"));
// Get all rows in tbody
List<WebElement> allRows = tbody.findElements(By.tagName("tr"));
// Get all cells for each row in allRows
for(WebElement row : allRows) {
List<WebElement> allCells = row.findElements(By.tagName("td"));
int rowNum = allRows.indexOf(row) + 1;
System.out.println("Row no. " + rowNum + " has " + allCells.size() +" cells.");
}
I have ASP.NET MVC4 project where try to parse html document with HtmlAgilityPack. I have the following HTML:
<td class="pl22">
<p class='pb10 pt10 t_grey'>Experience:</p>
<p class='bold'>any</p>
</td>
<td class='pb10 pl20'>
<p class='t_grey pb10 pt10'>Education:</p>
<p class='bold'>any</p>
</td>
<td class='pb10 pl20'>
<p class='pb10 pt10 t_grey'>Schedule:</p>
<p class='bold'>part-time</p>
<p class='text_12'>2/2 (day/night)</p>
</td>
I need to get values:
"any" after "Experience:"
"any" after "Education:"
"part-time", "2/2 (day/night)" after "Schedule:"
All what I imagine is that
HtmlNode experience = hd.DocumentNode.SelectSingleNode("//td[#class='pl22']//p[#class='bold']");
But it get me different element, which place in the top of the page. My Experience, Education and Schedule is static values. In additional my any, any part-time day/night is the dynamic values. Can anybody help me?
Below is an alternative which is more focused on the table headers (Experience, Education and Schedule), instead of the node classes:
private static List<string> GetValues(HtmlDocument doc, string header) {
return doc.DocumentNode.SelectNodes(string.Format("//p[contains(text(), '{0}')]/following-sibling::p", header)).Select(x => x.InnerText).ToList();
}
You can call that method like this:
var experiences = GetValues(doc, "Experience");
var educations = GetValues(doc, "Education");
var schedules = GetValues(doc, "Schedule");
experiences.ForEach(Console.WriteLine);
educations.ForEach(Console.WriteLine);
schedules.ForEach(Console.WriteLine);
You could do it something like this if you want to keep the XPath
var html = "<td class='pl22'><p class='pb10 pt10 t_grey'>Experience:</p><p class='bold'>any</p></td><td class='pb10 pl20'><p class='t_grey pb10 pt10'>Education:</p><p class='bold'>any</p></td><td class='pb10 pl20'><p class='pb10 pt10 t_grey'>Schedule:</p><p class='bold'>part-time</p><p class='text_12'>2/2 (day/night)</p></td> ";
var doc = new HtmlDocument
{
OptionDefaultStreamEncoding = Encoding.UTF8
};
doc.LoadHtml(html);
var part1 = doc.DocumentNode.SelectSingleNode("//td[#class='pl22']/p[#class='bold']");
var part2 = doc.DocumentNode.SelectNodes("//td[#class='pb10 pl20']/p[#class='bold']");
foreach (var item in part2)
{
Console.WriteLine(item.InnerText);
}
var part3 = doc.DocumentNode.SelectSingleNode("//td[#class='pb10 pl20']/p[#class='text_12']");
Console.WriteLine(part1.InnerText);
Console.WriteLine(part3.InnerText);
Output :
any
part-time
any
2/2 (day/night)
Below is my code:
#{
Layout = "/_SiteLayout.cshtml";
var db = Database.Open("MyDatabase");
var query = "SELECT * FROM Team";
var Teams = db.Query(query);
}
<form>
<table>
<tr>
<td>Team Name</td>
<td>Played</td>
<td>Points</td>
</tr>
#{ foreach(var Team in Teams){
<tr>
<td>#Team.TeamName</td>
<td><input type="text" value="#Team.Played" name="Played"/></td>
<td><input type="text" value="#Team.Points" name="Points"/></td>
</tr>
}
}
</table>
</form>
This is the result:
So what I want to do is update my whole table.
What is the SQL query to do this? I want to update Points and Games Played in my database for all teams once the form is posted.
I don't actually understand what exactly you are trying to achieve (update your whole table with what?), but here is some information you might find useful:
SQL Update Tutorial, SQL Update, Update from Select
Following is My Solution. Anyone have an efficient Solution?
#{
var db = Database.Open("MYDATABSE");
var query = "SELECT * FROM Team";
var Teams = db.Query(query);
var InsertQuery = "";
if(IsPost){
foreach(var Team in Teams){
var Points = Request[Team.TeamName];
var TeamId = Team.TeamId.ToString();
var Played = Request[TeamId];
var executeQueryString="UPDATE Team Set Points=#0, Played=#1 WHERE TeamId=#2";
db.Execute(executeQueryString, Points, Played, Team.TeamId);
}
Response.Redirect("~/UpdateTable.cshtml");
}
}
<br /><br />
<form action="" method="post">
<table>
<tr>
<td><h5>Team Name</h5></td>
<td><h5>Played</h5></td>
<td><h5>Points</h5></td>
</tr>
#{ foreach(var Team in Teams){
<tr>
<td>#Team.TeamName</td>
<td><input type="text" value="#Team.Played" name="#Team.TeamId"/></td>
<td><input type="text" value="#Team.Points" name="#Team.TeamName"/></td>
</tr>
}
}