actionscript 3 - how to get all properties from my Object? - actionscript-3

How to get all properties from my Object ?
My object is:
var myObj: Object = {
stringOne: "One",
stringTwo: "Two",
intOne: 1,
intTwo: 2
}

Try with this:
for(var key:* in myObj) {
trace (key + ':' + myObj[key]);
}

Related

JSON to OData String for Query

How can I turn a JSON object, i.e. { username: "john", password: "1234" } into an OData string query in a function using typescript? I could not find a library to do this for me (Angular 6). Here is my attempt:
function ConvertToODataString (json: Object) {
let ret_str: string = "";
for (let key in json) {
ret_str += (key + "=" + json[key] + "&");
}
if (ret_str) {
ret_str = ret_str.substr(0, ret_str.length - 1); // remove last &
}
return ret_str;
}
Does anyone know of a better way? For now, my json is not multi-leveled.
You can use for ... in to enumerate the object properties, adding each key/value pair to an array, and combine the values with Array.join:
function convertObjectToQuery(obj: Object): string {
let values = new Array<string>();
for (let prop in obj) {
values.push(`${prop} eq '${obj[prop]}'`);
}
return encodeURI("$filter=" + values.join(" and "));
}
See this stackblitz for a demo.
JSON.parse function.
Example:
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');
json={ "name":"John", "age":30, "city":"New York"};
var obj = JSON.parse(json+'');
I decided to use the HttpParms module instead:
import { HttpParams } from "#angular/common/http";
const params = new HttpParams()
.set("$filter", "Username eq '" + parameters["Username"] + "' and Password eq '" + parameters["Password"] + "'")
.set("$count", "true");
console.log(params.toString());

Flutter get Object property Name

I passed the following object:
var myVar = { typeA: { option1: "one", option2: "two" } }
I want to be able to pull out the key typeA from the above structure.
This value can change each time so next time it could be typeB.
So I would like to know if there is any way to do that
I was able to solve using 'keys'
for a json example like this:
{
"1-0001": {
"name": "red",
"hex": "FF0000"
},
"1-0002": {
"name": "blue",
"hex": "0000FF"
},
"1-0003": {
"name": "green",
"hex": "008000"
}
}
I was able to use
Map<String, dynamic> decoded = json.decode(jsonString);
for (var colour in decoded.keys) {
print(colour); // prints 1-0001
print(decoded[colour]['name']); // prints red
print(decoded[colour]['hex']); // prints FF0000
}
To get all filenames you can use:
var data = ...
var filenames = [];
for(var i = 0; i < data.length; i++) {
var item = data[0]['files'];
var key = item.keys.first;
var filename = item[key]['filename'];
filenames.add(filename);
}
print(filenames);
You need to define a data type.
It is basically a map of (key value-pair) where key is changed as stated in question typeA or typeB
This Object has 2 properties option1 and option2 which is also strings.
Here is the sample code to construct model and how to use it
import 'package:TestDart/TestDart.dart' as TestDart;
main(List<String> arguments) {
var map = new Map<String, MyObject>();
map['typeA'] = new MyObject("one", "two");
map['typeB'] = new MyObject("one", "two");
print(map['typeA'].toString());
print(map['typeA'].toString());
}
class MyObject {
String _option1;
String _option2;
MyObject(this._option1, this._option2);
String get option2 => _option2;
String get option1 => _option1;
#override
String toString() {
return 'MyObject{option1: $_option1, option2: $_option2}';
}
}
Relevant answer
map.forEach((key, value) {
print("Key : ${key} value ${value}");
});

how to get a key value from Json object in node js

I want to get the id value from the following json object
answerTag:
[ '[{"id":64,"name":"Coronary Artery Disease"}]',
'[{"id":64,"name":"Coronary Artery Disease"}]' ],
risk: '1' }
I had the same issue. I found that I had to remove the leading and trailing brackets before calling JSON.parse for it to work. The JSON.parse inside a try-catch didn't fail but just the same I couldn't access the values in the resultant object.
Here's the excerpt. "rows[0]" is the the first row of my MySQL result array
result = JSON.stringify(rows[0])
result = result.replace(/(^\[)/, '');
result = result.replace(/(\]$)/, '');
try {
var resultObj = JSON.parse(result);
} catch (e) {
console.log("Error, not a valid JSON string");
}
var my_value = resultObj["my_key"];
Hope this helps!
You dont have a valid JSON
as a valid json should have a key-value pair
var answerTag=
{
"key1":{"id":64,"name":"Coronary Artery Disease"},
"key2":{"id":64,"name":"Coronary Artery Disease"},
"key3":{risk: '1' }
}
So if u want to traverse that ,put it in for loop like this
getKeyValueFromJSON(answerTag);
function getKeyValueFromJSON(
for(var key in obj) {
alert("Key: " + key + " value: " + obj[key]);
}
}
Or if i am able to get it
The formed JSON is like this
var obj={
answerTag: [
'[{"id":64,"name":"Coronary Artery Disease"}]',
'[{"id":64,"name":"Coronary Artery Disease"}]' ],
risk: '1'
}
So You can use it like this
for(var key in obj) {
for(var innerKey in obj[key]) {
console.log("Key: " + innerKey + " value: " + obj[key][innerKey]);
}
}

Json object with random key value

I am developing an application, where I drag and drop images to grid, and after dropping I am creating a json object where I am adding that drop images to json object, there is a 144 square grid.
My json object gets created with entries:
27: Object
51: Object
54: Object
75: Object
99: Object
123: Object
125: Object
How can I loop through my exact key numbers in jquery for match with other json object?
Sample Example:
var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };
jQuery.each( arr, function( i, val ) {
$( "#" + val ).text( "Mine is " + val + "." );
// Will stop running after "three"
return ( val !== "three" );
});
jQuery.each( obj, function( i, val ) {
$( "#" + i ).append( document.createTextNode( " - " + val ) );
});
Output:
Mine is one. - 1
Mine is two. - 2
Mine is three. - 3
- 4
- 5
You can loop over object's properties like this:
var obj = {1: 'val1', 2:'val2', 3:'val3' }
for (var param in obj) {
if (obj.hasOwnProperty(param))
console.log(obj[param])
}
And this will print:
val1
val2
val3

How to create a `String[]` field with active annotation of xtend?

I tried with active annotation of xtend, and I want to create a live annotation which can generate a String[] field to record the names of method parameters.
#Target(ElementType::TYPE)
#Active(typeof(ParameterRecorderProcessor))
annotation ParameterRecorder {
}
class ParameterRecorderProcessor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {
var iii = 0;
// add the public methods to the interface
for (method : annotatedClass.declaredMethods) {
if (method.visibility == Visibility::PUBLIC) {
iii = iii + 1
annotatedClass.addField(method.simpleName + "_" + iii) [
type = typeof(String[]).newTypeReference // String[] doesn't work
var s = ""
for (p : method.parameters) {
if(s.length > 0) s = s + ","
s = s + "\"" + p.simpleName + "\""
}
val ss = s
initializer = [
'''[«ss»]'''
]
]
}
}
}
}
You can see I use typeof(String[]).newTypeReference to define the type of new created field, but it doesn't work. The generated java code is looking like:
private Object index_1;
It uses Object and the initializer part has be empty.
How to fix it?
This looks like a bug to me. As a workaround, you may want to use typeof(String).newTypeReference.newArrayTypeReference or more concise string.newArrayTypeReference