I am unable to get environment names from an array - javascript

I have an array in which the environment list is stored and from this list, I have to get all the environment names which I am unable to get right now. See below what I have tried and please help me out of this.
<script>
//${environmentList} is getting from backend i.e from modal class in which toString() method has stored with all its contents.
var envLength = `${environmentList}`.length;
var allEnv = `${environmentList}`;
console.log("All Environments : " + allEnv);
console.log("Length of environments : " + envLength);
for(let i = 0 ; i < envLength ; i++){
let all = `${allEnv[i].environmentName}`;
console.log("Environment Name : " + all[i]);
}
</script>
toString() method :
#Override
public String toString() {
return "EnvironmentModel [environmentId=" + environmentId + ", environmentName=" + environmentName
+ ", environmentDesc=" + environmentDesc + ", hostAddress=" + hostAddress + ", environmentURL="
+ environmentURL + ", configFileName=" + configFileName + ", configFilePath=" + configFilePath
+ ", companyId=" + companyId + ", createdBy=" + createdBy + ", createdAt=" + createdAt
+ ", lastUpdatedBy=" + lastUpdatedBy + ", lastUpdatedAt=" + lastUpdatedAt + ", elementFileName="
+ elementFileName + ", elementFilePath=" + elementFilePath + ", envStatus=" + envStatus + ", projectId="
+ projectId + ", project=" + project + "]";
}
OUTPUT:
All Environments :
[EnvironmentModel [environmentId=345, environmentName=MM_Env_1, environmentDesc=Environment_1, hostAddress=null, environmentURL=null, configFileName=SystemFile.xml, configFilePath=/env_files/MM_Env_1, companyId=1, createdBy=2, createdAt=2019-12-18 16:39:38.595, lastUpdatedBy=2, lastUpdatedAt=2019-12-18 16:42:13.259, elementFileName=ElementFile.xml, elementFilePath=/env_files/MM_Env_1, envStatus=1, projectId=null, project=[Project [projectName=Magic Matrix, projectDescription=Magic Matrix is a significant and robust tool of the Solitera Software Company., projectId=23]]], EnvironmentModel [environmentId=346, environmentName=MM_Env_2, environmentDesc=Environment_2, hostAddress=null, environmentURL=null, configFileName=soapAPI_request_GetLyrics.xml, configFilePath=/env_files/MM_Env_2, companyId=1, createdBy=2, createdAt=2019-12-18 16:43:10.59, lastUpdatedBy=2, lastUpdatedAt=2019-12-18 16:43:58.522, elementFileName=Element.xml, elementFilePath=/env_files/MM_Env_2, envStatus=1, projectId=null, project=[Project [projectName=Magic Matrix, projectDescription=Magic Matrix is a significant and robust tool of the Solitera Software Company., projectId=23]]], EnvironmentModel [environmentId=347, environmentName=TM_Env_1, environmentDesc=TM_Env_1, hostAddress=null, environmentURL=null, configFileName=showcasetestData.xml, configFilePath=/env_files/TM_Env_1, companyId=1, createdBy=2, createdAt=2019-12-18 16:44:33.783, lastUpdatedBy=2, lastUpdatedAt=2019-12-18 16:44:47.09, elementFileName=soapAPI_request_TempConvert.xml, elementFilePath=/env_files/TM_Env_1, envStatus=1, projectId=null, project=[Project ...
Length of environments : 2235
Environment Name : undefined

Use Jackson json parser in Java class and in javascript side use JSON.parse to change the string to JSON object.

On the javascript side you'll should understand that the variable environmentList is a string and the length of the string is 2235 chars. To access each property you'll have to parse the string and turn it into an object.
As you are not relying on JSON you need to use String.split or a regex matcher.
But the simpler solution would be to convert the Environment variables into a JSON format. As the type of the output from System.getenv() is a Map<string, string> you can try this solution to create a JSON structure: Converting Map<String,String> into json
After that you'll either write the json in a script tag in HTML so it get's parsed directly, or user JSON parse on the string of the above solution.

Related

How to get a JSON object that is parseable by TypedJSOn and class-transformer

I have tried using both class-transformer and TypedJson and both only work with using an object declared in this form:
let productJson = {
name: 'Aerosol',
productId: 'QSRT001',
description: 'A great product, air purifier'
}
However, I need to read the file of products from the file system. Both libraries are unable to work with the JSON because it is escaped with \\ for the quoted strings. Is there a way to unescaped the JSON read from the file so it can be represented in the productJson above? Please, please help!! It's hard to capture the output as it is very large. I removed most of the content, this is a small sample.
"{ \"name\" : \"John Smith\",\n" +
" \"sku\" : \"20223\",\n" +
" \"price\" : 23.95,\n" +
" \"shipTo\" : { \"name\" : \"Jane Smith\",\n" +
" \"address\" : \"123 Maple Street\",\n" +
" \"city\" : \"Pretendville\",\n" +
" \"state\" : \"NY\",\n" +
" \"zip\" : \"12345\" },\n" +
" \"billTo\" : { \"name\" : \"John Smith\",\n" +
" \"address\" : \"123 Maple Street\",\n" +
" \"city\" : \"Pretendville\",\n" +
" \"state\" : \"NY\",\n" +
" \"zip\" : \"12345\" }\n" +
"}\n" +
"\n";
I have been able to get a better representation. I need to be able to let the object get the TypedJSON to be able to process the file. if the file has the \ it is not processed by the TypedJSON correctly.
Is it possible to get it in the form like in productJson?

Concat QML Properties with Javascript

I'm setting properties on QML items with java script, where "gaugetext" is a property of my QML object.
function finishCreation(setWidth,setHeight,setX,setY,setMinValue,setMaxValue,setDecPlace,setUnit,setID,SetValueObject,SetValueProperty) {
...
"gaugetext": Qt.binding(function(){return SetValueObject[SetValueProperty] + " " + setUnit})
....
}
This works well, but I want to set numbers of decimals. So I tried this:
"gaugetext": Qt.binding(function(){return "(" + SetValueObject[SetValueProperty]+ ")" + ".toFixed(" + setDecPlace + ")" + " " + setUnit})
But this results that the correct Value is shown with ".toFixed(value)".
You return a String "(<someValue>).toFixed(<someOtherValue>) <someThirdValue>".
You probably want to omit some of your " since you probably want to have the pieces that you mark as string to be executed instead.
return SetValueObject[SetValueProperty].toFixed(setDecPlace) + " " + setUnit
~~~(untetested)
Most likely however using a "finalize creation" function is a bad approach.

parsing JSON data with java script

I use Google plus API and get this data from it and get an error while parsing this JSON data
and here is the code I use to parse this data and get error as data object is tested and work fine and hold data as it appear in past 2 images
var allIems = data.items;
for (var element in allIems) {
document.getElementById('datafromapi').innerHTML +=
(" , published time :" + element.published +
" , last updated time :" + element.updated +
", complete url : " + element.url
);
var obj = element.object.attachments;
document.getElementById('datafromapi').innerHTML +=
(+"\nattachments of post :\n" +
" type : " +
obj[0].objectType +
" ,displayName of content : " +
obj[0].displayName +
" ,content URL : " +
obj[0].url +
" ,content data :" +
obj[0].image.url +
" ,content type : " +
obj[0].image.type +
" ,content height : " +
obj[0].image.height +
" ,content width : " +
obj[0].image.width +
"\n\n\n");
}
});
i got that error appear
Uncaught TypeError: Cannot read property 'attachments' of undefined
element values in
for (var element in allIems) {
are keys of allItems, which in this case are array indices. You have to address the actual array items like this:
var obj = allItems[element].object.attachments;
Your code element.object.attachments; tries to access property object of a number, which does not exist.
Since we know that allItems is an array, you could have written:
for (var i = 0; i < allIems.length; i++) {
var obj = allItems[i].object.attachments;
Javascript has a built in JSON parser you can use that takes in a string of the data and returns an object.
let jsonDataAsString = "{\"a\":1,\"b\":2}";
let jsonDataAsObject = JSON.parse(jsonDataAsString);
Then you can traverse through the data as an object, referencing properties using dot-notation
console.log(jsonDataAsObject.a); // 1
To be safe, you should be comparing properties to null before trying to use then
if(jsonDataAsObject.somePropery != null) {
// the property exists so you can access it here
}

Escape special characters in javascript

$.ajax({
url: "/Search/GetMatchCount?idx=" + idx + "&q=" + escape(q) + "&filter=" + escape(filter) + "&fields=" + fields,
success: function (data) {
$("#tabprograms").html("Customer Programs (" + data + ")");
}
});
I have a string "civic+governance" in my search text-box.
I want to retrieve the value of the text-box in some local variable with jquery/javascript. My code is,
var q = document.getElementById('q').value;
The value that I get for q is "civic governance".
The '+' sign seems to be encoded with " ".
How do I not make it encoded to " " and have my string as it is , i.e. "civic+governance".
Your problem is that you are using ESCAPE(q). ESCAPE can not encode the + character you need to use the encodeURIComponent(q) then decode in php with rawurldecode() or you could also change your method to POST.

Pass multiple parameters from ASP.NET to javascript function

Essentially what I'm trying to do is fairly simple....pass more than one parameter (4 total eventually) into a javascript function from my ASP.NET code behind.
What I've tried to do is this in the ASCX file...
function ToggleReportOptions(filenameText, buttonText) { /*stuff happens here*/ }
and this in the ASCX.cs file...
string testing123 = "testStringOne";
string testing124 = "testStringTwo";
optReportOptionsRunRealTime.Attributes["onClick"] = "ToggleReportOptions('" + testing123 + ", " + testing124 + "')";
optReportOptionsOffline.Attributes["onClick"] = "ToggleReportOptions('" + testing123 + ", " + testing124 + "')";
but this doesn't seem to work as in my output the first variable contains "testStringOne, testStringTwo" and the 2nd variable is "undefined"
Any help on clearing up my probably stupid issue here would be great (i'm very inexperienced with javascript, more of a .NET developer)
You've missed out a few single-quotes, meaning that you're passing a single string containing a comma rather than two separate strings. That is, you're passing 'testStringOne, testStringTwo' rather than 'testStringOne' and 'testStringTwo'.
Try this instead:
optReportOptionsRunRealTime.Attributes["onClick"] =
"ToggleReportOptions('" + testing123 + "', '" + testing124 + "')";
optReportOptionsOffline.Attributes["onClick"] =
"ToggleReportOptions('" + testing123 + "', '" + testing124 + "')";

Categories

Resources