Iterating through nested arrays with hyphen in object name - arrays

First of all, I know naming array objects with hyphens is completely incorrect, and I'm not the creator of this. I need to call an API within a service, and many objects are named improperly, like {"children-education": [ and { "Kid Stories": [.
I have tried assigning the name to a variable like let edChild = "child-education"and then parsing it to an object, like edChild = JSON.parse(edChild)to no avail. I really have no idea of what I'm doing, nor even if it's possible to do so.
I kinda have the option to call my customer and kindly ask his team to rename the objects to something less... stupid than special characters than I can't possible call in Typescript, but I'd like to learn if there's a way to surpass this in future occasions or if they can't rename it.
Here's an example of the JSON I'm trying to iterate through:
{
"business":
[
{
"anim":
[
{
"child-education": [
{
"Kid Stories": [
{
"id": 1,
"name": "Three Little Pinkies",
"url": "#",
"description": "shows how the world is beautiful"
},
(... and so on)
Thanks in advance.

You can parse that JSON string like any JSON string, using JSON.parse()
let str = "{\n" +
" \"business\":\n" +
" [\n" +
" {\n" +
" \"anim\":\n" +
" [\n" +
" {\n" +
" \"child-education\": [\n" +
" {\n" +
" \"Kid Stories\": [\n" +
"\n" +
" {\n" +
" \"id\": 1,\n" +
" \"name\": \"Three Little Pinkies\",\n" +
" \"url\": \"#\",\n" +
" \"description\": \"shows how the world is beautiful\"\n" +
" }]}]}]}]}" +
""
console.log(JSON.parse(str));
After that, you can use bracket notation to access any property names
let str = "{\n" +
" \"business\":\n" +
" [\n" +
" {\n" +
" \"anim\":\n" +
" [\n" +
" {\n" +
" \"child-education\": [\n" +
" {\n" +
" \"Kid Stories\": [\n" +
"\n" +
" {\n" +
" \"id\": 1,\n" +
" \"name\": \"Three Little Pinkies\",\n" +
" \"url\": \"#\",\n" +
" \"description\": \"shows how the world is beautiful\"\n" +
" }]}]}]}]}" +
""
const obj = JSON.parse(str);
console.log(obj.business[0].anim[0]['child-education'][0]['Kid Stories'][0]);

Related

longPointerPress() not called for (most?) components

I noticed that longPointerPress is not called for Label derivates.
Therefore I created some code to investigate further when longPointerPress() is called and was surprised that:
Label derivates do receive pointer events - however no longPointerPress
Button derivates do receive pointer events and longPointerPress just works
TextField derivates do receive pointer events unless editing, then they eat all the pointer events.
What I'd like to achieve is to be able to have various components in a Container and still be able to get notified for longPointerPress events under all circumstances.
How can I achieve this?
Here is the code - do a long tap on all the components and watch the console:
public class FormLongPointerPress extends Form {
#SuppressWarnings("unchecked")
private interface With<T> {
void act(T ... aTs);
}
public FormLongPointerPress() {
super("FormLongPointerPress");
setScrollableY(true);
setLayout(BoxLayout.y());
With<Component> with = (aComponents) -> {
Container container = FlowLayout.encloseCenter(aComponents);
container.addPointerPressedListener((aActionEvent) -> Log.p("container.pointerPressed(" + aActionEvent.getX() + ", " + aActionEvent.getY() + ")"));
container.addPointerReleasedListener((aActionEvent) -> Log.p("container.pointerReleased(" + aActionEvent.getX() + ", " + aActionEvent.getY() + ")"));
container.addLongPressListener((aActionEvent) -> Log.p("container.longPress(" + aActionEvent.getX() + ", " + aActionEvent.getY() + ")"));
for (Component component: aComponents) {
component.addPointerPressedListener((aActionEvent) -> Log.p("component.pointerPressed(" + aActionEvent.getX() + ", " + aActionEvent.getY() + ")"));
component.addPointerReleasedListener((aActionEvent) -> Log.p("component.pointerReleased(" + aActionEvent.getX() + ", " + aActionEvent.getY() + ")"));
component.addLongPressListener((aActionEvent) -> Log.p("component.longPress(" + aActionEvent.getX() + ", " + aActionEvent.getY() + ")"));
}
getContentPane().add(container);
};
with.act();
with.act(new Label("A Label"));
with.act(new Button("A Button"));
with.act(new TextField("A TextField"));
}
}

How to use radio button in html and javascript to insert into the database in CN1

I was trying to get the values of radio button in html page when checked and insert it into the sqlite database and mysql. This is my full code below:
Form hi = new Form("Hi World");
final WebBrowser b = new WebBrowser(){
#Override
public void onLoad(String url) {
// Placed on onLoad because we need to wait for page
// to load to interact with it.
BrowserComponent c = (BrowserComponent)this.getInternal();
// Create a Javascript context for this BrowserComponent
JavascriptContext ctx = new JavascriptContext(c);
JSObject window = (JSObject)ctx.get("window");
window.set("addAsync", new JSFunction(){
public void apply(JSObject self, final Object[] args) {
String a = (String)args[0];
// Double b = (Double)args[1];
System.out.println("Value picked is "+a);
JSObject callback = (JSObject)args[1];
callback.call(new Object[]{new String(a)});
System.out.println("ok Good");
}
});
}
};
b.setPage( "<html lang=\"en\">\n" +
" <head>\n" +
" <meta charset=\"utf-8\">\n" +
" <script>\n" +
"function test() {"+
" var radios = document.getElementsByName('radiotest');"+
" var found = 1;"+
" for (var i = 0; i < radios.length; i++) { " +
" if (radios[i].checked) {"+
// " document.getElementById('input3').setAttribute('value', radios[i].value); " +
" alert(radios[i].value);"+
" found = 0;"+
" break;"+
" }"+
"}"+
" if(found == 1)"+
" {"+
" alert('Please Select Radio');"+
" } "+
"}"+
// + \n" +
" var radios = document.getElementsByName('radiotest')"+
" document.addEventListener('click', function(){\n" +
" var found = 1;"+
" for (var i = 0; i < radios.length; i++) { " +
" if (radios[i].checked) {"+
// " document.getElementById('input3').setAttribute('value', radios[i].value); " +
" alert(radios[i].value);"+
" found = 0;"+
" break;"+
" }"+
"}"+
" if(found == 1)"+
" {"+
" alert('Please Select Radio');"+
" } "+
" a = document.getElementById('val1').value ;\n" +
" b = document.getElementById('val2').value\n" +
" window.addAsync(a, b, function(result){\n" +
" document.getElementById('result').innerHTML = result;\n" +
" });\n" +
" }, true); "
+ "" +
" </script>\n" +
" </head>\n" +
" <body >\n" +
" <table border='0' cellspacing = '15'>"+
" <thead>"+
" </thead>"+
" <tbody>"+
" <tr>"+
" <td><font size = '3'> <b>A </font></b><input type='radio' name='radiotest' id='val1' onclick='test()' value='1' style='border: none;'> value1</input>"+
"</td>"+
" </tr>"+
" <tr>"+
" <td><font size = '3'> <b> B</font></b><input type='radio' name='radiotest' id='val2' onclick='test()' value='2' style='border: none;'> value2</input>"+
"</td>"+ " </tr>"+
" <tr>"+
" <td> <font size = '3'> <b>C </font></b><input type='radio' name='radiotest' id='val3' onclick='test()' value='3'style='border: none;'> value3</input>"+
" </tr>"+
"<tr>"+
" <td> <font size = '3'> <b>D </font></b> <input type='radio' name='radiotest'id='val4' onclick='test()' value='4'style='border: none;'> value4</input>"+
" </tr>"+
"</tbody>"+
"</table>"+
"<span id=\"result\"></span>"+
" </body>\n" +
"</html>", null);
hi.setLayout(new BorderLayout());
hi.setLayout(new BorderLayout());
hi.addComponent(BorderLayout.CENTER, b);
hi.show();
The code above did not work for me. Pls help.
The problem am having now is to add actionListener to the radio button when checked by collecting its value and insert these values into the database accordingly.
I have edited my code, With this I can get the value selected with javascript function with the code below. Now, How can I insert the values of the radio button into the database as I click on radio button.
Form hi = new Form("BrowserComponent", new BorderLayout());
BrowserComponent bc = new BrowserComponent();
bc.setPage( "<html lang=\"en\">\n" +
" <head>\n" +
" <meta charset=\"utf-8\">\n" +
" </head>\n" +
" <body >\n" +
" <table border='0' cellspacing = '15'>"+
" <thead>"+
" </thead>"+
" <tbody>"+
" <tr>"+
" <td><font size = '3'> <b>A </font></b><input type='radio' name='radiotest' id = 'val1' onclick='test()' value='1' style='border: none;'> value1</input>"+
"</td>"+
" </tr>"+
" <tr>"+
" <td><font size = '3'> <b> B</font></b><input type='radio' name='radiotest'id = 'val2' onclick='test()' value='2' style='border: none;'> val2</input>"+
"</td>"+
" </tr>"+
" <tr>"+
" <td> <font size = '3'> <b>C </font></b><input type='radio' name='radiotest' onclick='test()' value='3'style='border: none;'> val3</input>"+
" </tr>"+
"<tr>"+
" <td> <font size = '3'> <b>D </font></b> <input type='radio' name='radiotest' onclick='test()' value='4'style='border: none;'> val4</input>"+
" </tr>"+
"</tbody>"+
"</table>"+
" <script type='text/javascript'>" +
//This javascript is perfect now.
"function test() {"+
" var radios = document.getElementsByName('radiotest');"+
" var found = 1;"+
" for (var i = 0; i < radios.length; i++) { " +
" if (radios[i].checked) {"+
// " document.getElementById('input3').setAttribute('value', radios[i].value); " +
// " instruction = document.getElementById('input3').value" +
// "'"+option_code2+"'"+
" alert(radios[i].value);"+
" found = 0;"+
" break;"+
" }"+
"}"+
" if(found == 1)"+
" {"+
" alert('Please Select Radio');"+
" } "+
"}"+
"</script>"+
" </body>\n" +
"</html>", null);
TextField tf = new TextField();
hi.add(BorderLayout.CENTER, bc).
add(BorderLayout.SOUTH, tf);
//bc.addWebEventListener("onLoad", (e) -> bc.execute("fnc('<p>Hello World</p>')"));
hi.show();
Make sure your libraries are up to date, if the JavaScript bridge works correctly for setURL but fails for setPage it sounds like something we fixed in a recent update.

Eliminating undesired characters from every string in an Array in swift

I have an array called coordinateArray that is holding this data:
["(19.452187074041884", " -99.1457748413086)", "(19.443769985032485", " -99.14852142333984)", "(19.443446242121073", " -99.13787841796875)", "(19.450244707639662", " -99.13822174072266)"]
["(19.407780723677718", " -99.18417591514299)", "(19.400373302640162", " -99.18473381461808)", "(19.400049473260434", " -99.18039936485002)", "(19.405433052977592", " -99.17838234367082)"]
["(19.4022042123319", " -99.1457748413086)", "(19.401070819438004", " -99.16139602661133)", "(19.39184146912981", " -99.16268348693848)", "(19.389736456288546", " -99.14706230163574)"]
["(19.42114689571205", " -99.17375564575195)", "(19.425598915444077", " -99.15392875671387)", "(19.414913863184566", " -99.15470123291016)", "(19.41264724664359", " -99.16594505310059)", "(19.412890099927345", " -99.16766166687012)"]
I need to remove the parenthesis from each coordinate
How can I do this in Swift 3.0?
I tried to do this
let coordinateArray = coor.components(separatedBy: ",")
var coordinateArrayF = [String]()
for coordinate in coordinateArray {
let coordinatevar = coordinate.replacingOccurrences(of: "()", with: "")
coordinateArrayF.append(coordinatevar)
}
But it isn't working what am I doing wrong?
Functional programming is your friend!
var data = ["(19.452187074041884", " -99.1457748413086)", "(19.443769985032485", " -99.14852142333984)", "(19.443446242121073", " -99.13787841796875)", "(19.450244707639662", " -99.13822174072266)"]
let cleanData = data.map {
item in item.replacingOccurrences(of: "(", with: "")
}

Issue looping through an array response

I was writing an API using the kat.cr API and the IMDB api too in nodejs, I didn't use json.stringify cuz I didn't know about it at the time of writing haha XD, anyway, the problem is that when I loop through the code in 46 through 50, the response remains the same here's an example,
here is the json generated,
{
"MovieList": [{
"title": "Jurassic World",
"imdb": "tt0369610",
"poster_med": "http://ia.media-imdb.com/images/M/MV5BMTQ5MTE0MTk3Nl5BMl5BanBnXkFtZTgwMjczMzk2NTE#._V1_SX300.jpg",
"poster_big": "http://ia.media-imdb.com/images/M/MV5BMTQ5MTE0MTk3Nl5BMl5BanBnXkFtZTgwMjczMzk2NTE#._V1_SX300.jpg",
"genres": ["Action, Adventure, Sci-Fi"],
"items": [{
"torrent_magnet": "magnet:?xt=urn:btih:9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892&dn=maze+runner+the+scorch+trials+2015+hc+720p+hdrip+x264+shaanig&tr=udp%3A%2F%2Ftracker.publicbt.com%2Fannounce&tr=udp%3A%2F%2Fopen.demonii.com%3A1337",
"torrent_seeds": "1262",
"torrent_peers": "1306",
"id": "9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892"
}]
}, {
"title": "San Andreas",
"imdb": "tt2126355",
"poster_med": "http://ia.media-imdb.com/images/M/MV5BNjI4MTgyOTAxOV5BMl5BanBnXkFtZTgwMjQwOTA4NTE#._V1_SX300.jpg",
"poster_big": "http://ia.media-imdb.com/images/M/MV5BNjI4MTgyOTAxOV5BMl5BanBnXkFtZTgwMjQwOTA4NTE#._V1_SX300.jpg",
"genres": ["Action, Drama, Thriller"],
"items": [{
"torrent_magnet": "magnet:?xt=urn:btih:9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892&dn=maze+runner+the+scorch+trials+2015+hc+720p+hdrip+x264+shaanig&tr=udp%3A%2F%2Ftracker.publicbt.com%2Fannounce&tr=udp%3A%2F%2Fopen.demonii.com%3A1337",
"torrent_seeds": "1262",
"torrent_peers": "1306",
"id": "9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892"
}]
}]
}
Here is the crawler code :
var kat = require('kat-api');
var IMDb = require('imdb-scraper');
var movieTitle = require('movie-title');
var nameToImdb = require("name-to-imdb");
var movie = require('node-movie');
var fs = require('fs');
var util = require('util');
var log_file = fs.createWriteStream(__dirname + '/main.json', {
flags: 'w'
});
var log_stdout = process.stdout;
var config = '720p 2015'; //This is the line that should be changed if needed!
console.log = function(d) { //
log_file.write(util.format(d) + '\n');
log_stdout.write(util.format(d) + '\n');
};
var kat = require('kat-api');
kat.search({
query: config,
category: 'movies',
language: 'en'
}).then(function(response) {
var quotes = '"';
var startingOfJson = "{" + quotes + "MovieList" + quotes + ":" + "[";
var endingOfJson = "}";
var itemStart = quotes + "items" + quotes + ":" + "[{";
var itemEnd = "}]";
console.log(startingOfJson);
for (i = 0; i <= 20; i++) {
var titleForEverything = movieTitle(response.results[i].title);
movie(titleForEverything, function(err, data) {
console.log("{");
console.log(quotes + "title" + quotes + ":" + quotes + data.Title + quotes + ",");
console.log(quotes + "imdb" + quotes + ":" + quotes + data.imdbID + quotes + ",");
console.log(quotes + "poster_med" + quotes + ":" + quotes + data.Poster + quotes + ",");
console.log(quotes + "poster_big" + quotes + ":" + quotes + data.Poster + quotes + ",");
var genres = quotes + "genres" + quotes + ":" + "[" + quotes + data.Genre + quotes + "]" + ",";
console.log(genres);
console.log(itemStart);
console.log(quotes + "torrent_magnet" + quotes + ":" + quotes + response.results[i].magnet + quotes + ",");
console.log(quotes + "torrent_seeds" + quotes + ":" + quotes + response.results[i].seeds + quotes + ",");
console.log(quotes + "torrent_peers" + quotes + ":" + quotes + response.results[i].peers + quotes + ",");
console.log(quotes + "id" + quotes + ":" + quotes + response.results[i].hash + quotes);
console.log(itemEnd);
if (i == 20) {
console.log("}");
} else {
console.log("},")
}
});
}
}).catch(function(error) {
console.log('an error occured' + error);
});
console.log("]}");
and you can see that the magnet, seeds, hash and peers remain the same for all the results generated! How can I fix this and why does this happen? Thank you! :D
You're committing the classic error of a function inside a loop closing over the loop index i; when the function is executed, i will already have its final value. The easiest way to fix this is with for (let i.

Pass Parameters To Dapper List

Need to know the best practice for passing multiple parameters to a dapper list function
I need to pass the #StartDate, #EndDate, #AgentId, #Crc passed into the posted method but not sure exactly how to do it using Dapper. Thanks in advance
public List<CallSearchRepositoryBO> GetCallSearchRecords(DateTime startDate, DateTime endDate, int agentId, string crc)
{
try
{
using (IDbConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CCWorkforceConnectionString"].ConnectionString))
{
var items = connection.Query<CallSearchRepositoryBO>
("SELECT h.historyid " +
" ,h.Dialid " +
" ,h.Crc " +
" ,c.szExternalId ExternalId " +
" ,a.AgentId " +
" ,UPPER(a.LastName +',' +a.FirstName) Agent" +
" ,c.ContactId" +
" ,CallDateTime" +
" ,h.ProjName Campaign" +
" ,h.PhoneNum PhoneNumber" +
" ,szQ01 Q1" +
" ,szQ02 Q2" +
" ,szQ03 Q3" +
" ,szQ04 Q4" +
" ,szQ05 Q5" +
" ,szQ06 Q6" +
" ,szQ07 Q7" +
" ,szQ08 Q8" +
" ,szQ09 Q9" +
" ,szQ10 Q10" +
" ,txtQ33 Comments" +
" ,szFaxType AlertType" +
" ,r.VoxFilePath+VoxFileName RecordingPath" +
" ,r.RecLength" +
" ,CASE WHEN DataLength(txtQ33) > 0 THEN 1 ELSE 0 END HasComments " +
" FROM Touchstar..History h" +
" INNER JOIN Touchstar..Agent a WITH(NOLOCK) ON h.AgentId = a.AgentId" +
" INNER JOIN Touchstar..Recording r WITH(NOLOCK) ON h.HistoryId = r.HistoryId" +
" LEFT JOIN Touchstar..Contact c WITH(NOLOCK) ON h.DialId = c.DialId" +
" LEFT JOIN Touchstar..Sales s WITH(NOLOCK) ON c.ContactId = s.ContactId" +
" WHERE h.CallDateTime BETWEEN #StartDate AND #EndDate" +
" AND h.AgentId=#AgentId"+
" AND h.Crc=#Crc," +
" AND c.szCampaignId IN('UP2','UP4')" +
" ORDER BY CallDateTime DESC").ToList();
return items;
}
}
catch (Exception)
{
throw;
}
}
Pass through an object as a second parameter to the Query function. Notice the extra parameter in the below code "new {StartDate = startDate, EndDate = endDate, AgentId = agentId, Crc = crc}"
var items = connection.Query<CallSearchRepositoryBO>
("SELECT h.historyid " +
" ,h.Dialid " +
" ,h.Crc " +
" ,c.szExternalId ExternalId " +
" ,a.AgentId " +
" ,UPPER(a.LastName +',' +a.FirstName) Agent" +
" ,c.ContactId" +
" ,CallDateTime" +
" ,h.ProjName Campaign" +
" ,h.PhoneNum PhoneNumber" +
" ,szQ01 Q1" +
" ,szQ02 Q2" +
" ,szQ03 Q3" +
" ,szQ04 Q4" +
" ,szQ05 Q5" +
" ,szQ06 Q6" +
" ,szQ07 Q7" +
" ,szQ08 Q8" +
" ,szQ09 Q9" +
" ,szQ10 Q10" +
" ,txtQ33 Comments" +
" ,szFaxType AlertType" +
" ,r.VoxFilePath+VoxFileName RecordingPath" +
" ,r.RecLength" +
" ,CASE WHEN DataLength(txtQ33) > 0 THEN 1 ELSE 0 END HasComments " +
" FROM Touchstar..History h" +
" INNER JOIN Touchstar..Agent a WITH(NOLOCK) ON h.AgentId = a.AgentId" +
" INNER JOIN Touchstar..Recording r WITH(NOLOCK) ON h.HistoryId = r.HistoryId" +
" LEFT JOIN Touchstar..Contact c WITH(NOLOCK) ON h.DialId = c.DialId" +
" LEFT JOIN Touchstar..Sales s WITH(NOLOCK) ON c.ContactId = s.ContactId" +
" WHERE h.CallDateTime BETWEEN #StartDate AND #EndDate" +
" AND h.AgentId=#AgentId"+
" AND h.Crc=#Crc," +
" AND c.szCampaignId IN('UP2','UP4')" +
" ORDER BY CallDateTime DESC", new {StartDate = startDate, EndDate = endDate, AgentId = agentId, Crc = crc}).ToList();

Resources