Issue while converting actionscript object into XML - actionscript-3

I'm new to flex. I'm trying to convert a complex object into an XML.
I'm facing a problem when i'm trying to traverse through the object. here i'm adding the code snippet.
Creating Employee Object
var result:String = new String();
employeeDept = new EmployeeDept();
employee = new Employee();
employee.employeeId = "56789";
employee.employeeName = "XYZ";
//tempString = tempString + "--- Step n ---";
/*employee.employeeDept.deptId = "2";
employee.employeeDept.deptName = "APPLE";*/
employeeDept=new EmployeeDept();
employeeDept.deptId = "3";
employeeDept.deptName = "MS";
employee.employeeDept.addItem(employeeDept);
employeeDept=new EmployeeDept();
employeeDept.deptId = "2";
employeeDept.deptName = "APPLE";
employee.employeeDept.addItem(employeeDept);
empCollection.addItem(employee);
Calling converter method on employee object
recursive(employee, "ShopServiceLifeCycleArtifacts");
Converter method
public function recursive(obj:Object, str:String):String
{
try
{
var xml:XML=new XML('<' + str + '></' + str + '>' );
if(obj is Array && obj.length!=0)
{ //tempString = tempString + "--- Recursive IF Loop---" + obj.length;
var ac:ArrayCollection=new ArrayCollection(obj as Array);
var xml2:XML=new XML(<dept></dept>);
for (var i:Number=0; i<ac.length; i++)
{
var myObj:Object=ac.getItemAt(i);
for (var prop:String in myObj)
{
xml2.appendChild(new XML("<" + prop + ">" + myObj[prop] + "</" + prop + ">"));
}
}
xml.appendChild(xml2);
} else {
if (obj==null)
tempString = tempString + "--- Obj is null ---";
else
{
tempString = tempString + "--- Obj is not null ---";
}
for (var property:String in obj)
{
tempString = tempString + property;
if(obj[property] is Array)
{
tempString = tempString + "--- obj[property] is Array---";
xml.appendChild(recursive(obj[property] as Array, property));
} else {
tempString = tempString + "--- obj[property] is not Array---";
xml.appendChild(XML("<" + property + ">" + obj[property].toString() + "</" + property + ">"));
}
}
}
}catch(e:Error)
{
return tempString+"----------"+e.message;
}
tempString = tempString +xml.toString();
return xml.toString();
}
}
But the issue now is, my employee object is not an array so it will go to else block. I'm check whether the object is null. Object is not null.
I'm going to next step i.e. for loop
for (var property:String in obj){
here comes the issue. Its not going into this for loop & returning null. :(
instead of creating employee object in an action script file & geting a instance of that employee in here if i declare & define an employeeName object here only then the logic is working. i will show that code also.
else {
//tempString = tempString + "--- Recursive Else Loop---";
var employeeNew:Object = {employeeId:"123", employeeName:"San Francisco",
empDept :[{empDeptId:"12345", empDeptName:"XYZ"},{empDeptId:"54568", empDeptName:"ABC"}]};
for (var prop:String in employeeNew) {
//trace("myObject."+prop+" = "+employeeNew[prop]);
if(employeeNew[prop] is Array)
{
xml.appendChild(recursive(employeeNew[prop] as Array, prop));
} else {
tempString = tempString + "--- obj[property] is not Array---";
xml.appendChild(XML("<" + prop + ">" + employeeNew[prop].toString() + "</" + prop + ">"));
}
}
If i use this object its able to convert it into XML. Plz do let me know what is the mistake i'm doing here because of which i'm not able to traverse through the object.
Thanks in Advance.

The for ... in loop only works on dynamic properties of objects. A dynamic property is one that can be added to the object/class at run time.
Your second approach works because you are iterating over an actual Object to which you have added properties to.
The first example does not work because you are iterating over an Employee object. The employee class is not marked as dynamic, and therefore it you cannot add additional properties to it. Even if you made it dynamic (which has a performance impact), it would only iterate over the additional properties that you added at run time, not the properties you define in the class.
One solution to this type of problem is to use describeType():
looping through object property names in actionscript
http://www.actionscript.org/forums/showthread.php3?t=183820

Related

How to get dynamic object properties

I'm receiving a Json object from a request and I would like to Iterate over its properties and do something like:
if property is equals to "EN" than get it's value.
The solutions That I saw in the web are all related with GetProperties/GetProperty methods but I tried both and none of them worked.
This should be something "simple" but I think that I'm missing something here.
//Deserializing the object
ExpandoObject deserializedContent = JsonConvert.DeserializeObject<ExpandoObject>(obj.ToString(), new ExpandoObjectConverter());
dynamic deserializedDynamicContent = deserializedContent;
//Tries
var value = deserializedDynamicContent.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
var value = deserializedDynamicContent.GetType().GetProperty("ES").GetValue();
in both cases I get zero properties.
I can only get the values if I do the code below, but this will obly me to code if a new language is added.
deserializedDynamicContent.EN,
deserializedDynamicContent.ES or
deserializedDynamicContent.PT
What Am I doing wrong here?
{
"EN":[{"Id":1,"Name":"One"},{"Id":2,"Name":"Two"},{"Id":3,"Name":"Tree"}],
"ES":[{"Id":1,"Name":"Uno"},{"Id":2,"Name":"Dos"},{"Id":3,"Name":"Tres"}],
"PT":[{"Id":1,"Name":"Um"},{"Id":2,"Name":"Dois"},{"Id":3,"Name":"TrĂªs"}]
}
I'm not sure exactly what you're trying to achieve, but since ExpandoObject implements IDictionary<string,object> you should be able to do something like this:
var expando = JsonConvert.DeserializeObject<ExpandoObject>(yourJson);
var dict = (IDictionary<string, object>)expando;
// look for a particular key...
object value;
if (dict.TryGetValue("EN", out value))
{
Console.WriteLine("Key exists!");
var list = (List<dynamic>)value;
Console.WriteLine(string.Join(",", list.Select(x => "{" + x.Id + "," + x.Name + "}")));
}
// or enumerate the entire dictionary...
foreach (var kvp in dict)
{
var list = (List<dynamic>)kvp.Value;
Console.WriteLine(
kvp.Key + ":" + string.Join(",", list.Select(x => "{" + x.Id + "," + x.Name + "}")));
}

Render json string from controller to mvc view

My json string is coming as follows:
"[{\"StartTime\":\"09:00\",\"Dates\":\"05-28-2015\",\"Code\":\"DF\",\"LocationCode\":\"NY\"},{\"StartTime\":\"09:30\",\"Dates\":\"05-28-2015\",\"Code\":\"DF\",\"LocationCode\":\"NY\"},{\"StartTime\":\"10:00\",\"Dates\":\"05-28-2015\",\"Code\":\"DF\",\"LocationCode\":\"NY\"},{\"StartTime\":\"10:30\",\"Dates\":\"05-28-2015\",\"Code\":\"DF\",\"LocationCode\":\"NY\"},{\"StartTime\":\"11:30\",\"Dates\":\"05-28-2015\",\"Code\":\"DF\",\"LocationCode\":\"NY\"}]"
I need to parse this json string on view and show the data in a table.
I am new to json. Any help will be really appreciated. Thanks.
$("#divLoad").load("GetAvailableTimeSlots?strProvider=" + provider + "&strFrom=" + from, function (data) {
var newStr = data.replace('"[', '').replace(']"', '').replace('[', '').replace(']', '');
var dataArr = newStr.split('},{');
var jsonArr = new Array(dataArr.length);
for (var i = 0; i < dataArr.length; i++) {
dataArr[i] = '{' + dataArr[i] + '}';
var dataElem = dataArr[i].replace('{{', '{').replace('}}', '}');
var jsonElem = "'" + dataElem + "'";
jsonArr[i] = JSON.parse(jsonElem);
}
$(this).html(jsonArr);
});
If you are using jQuery, you can use the jQuery.parseJSON() method.
http://api.jquery.com/jquery.parsejson/
Just call JSON.parse(data) and manipulate the javascript object instead of performing error-prone string manipulation.
function (data) {
var locations = JSON.parse(data);
var table = $("<ul>");
for (var i = 0; i < locations.length; i++) {
var row = $("<li>").text(locations[i].StartTime + " " + locations[i].Dates + " " ...);
table.append(row);
}
$("#divLoad").empty().append(table); // $(this) won't work
}
This example is using an unordered list but you can easily replace this with table markup.
Here's what it looks like in Chrome dev tools with console.log(locations) after parsing:

Sorted Object/Map in AS3 Flex?

Is it possible to sort an object or have some sort of order in an object in Actionscript 3 Flex? I have an object which is created like so:
private var data:Object = new Object();
I then go on to put some keys and values into it:
this.data.someKeyOne = 0;
this.data.someKeyTwo = 1337;
this.data.someKeyThree = 123;
After that I loop through the object:
for (var dataPart:String in this.data) {
trace("Key: " + dataPart = " Value:" + this.data["dataPart"]);
}
But my issue is that the data will not be displayed in the order I initialized them in. This is frustrating as it is required to be in the order initialized.
As #helloflash said in his answer about loop through an object, and according to Adobe's definition of associative array (which is an instance of the Object class) :
An associative array, sometimes called a hash or map, uses keys instead of a numeric index to organize stored values. Each key in an associative array is a unique string that is used to access a stored value. An associative array is an instance of the Object class, which means that each key corresponds to a property name. Associative arrays are unordered collections of key and value pairs. Your code should not expect the keys of an associative array to be in a specific order.
The behavior that you got is normal and you can not get ordered keys in your case (using an Object like that).
If you don't need keys, you can do as #helloflash said in his answer, using a simple Array or Vector. Otherwise you can use an Array of objects, like this :
var obj:Object, i:int, s:String;
var data_array:Array = [
{key: 'someKeyOne', value: 0},
{key: 'someKeyTwo', value: 1337},
{key: 'someKeyThree', value: 123}
]
for(i = 0; i < data_array.length; i++){
obj = data_array[i];
trace('Key : ' + obj.key + ', Value = ' + obj.value);
}
for (s in data_array) {
obj = data_array[s];
trace('Key : ' + obj.key + ', Value = ' + obj.value);
}
for each (obj in data_array){
trace('Key : ' + obj.key + ', Value = ' + obj.value);
}
// all these 3 loops give :
// Key : someKeyOne, Value = 0
// Key : someKeyTwo, Value = 1337
// Key : someKeyThree, Value = 123
Or you can use Object and then sort it into an Array like this :
var data_object:Object = new Object();
data_object.someKeyOne = {index:0, value: 0};
data_object.someKeyTwo = {index:1, value: 1337};
data_object.someKeyThree = {index:2, value: 123};
var tmp_array:Array = sort_obj(data_object);
for(i = 0; i < tmp_array.length; i++){
obj = tmp_array[i];
trace('Key : ' + obj.key + ', Value = ' + obj.value);
}
for (obj in tmp_array) {
obj = tmp_array[obj];
trace('Key : ' + obj.key + ', Value = ' + obj.value);
}
for each (obj in tmp_array){
trace('Key : ' + obj.key + ', Value = ' + obj.value);
}
function sort_obj(obj:Object):Array {
var a:Array = [];
for (var key:String in obj) {
a.push({key: key, index: obj[key].index, value: obj[key].value});
}
a.sortOn('index', Array.NUMERIC);
return a;
}
// all these 3 loops give :
// Key : someKeyOne, Value = 0
// Key : someKeyTwo, Value = 1337
// Key : someKeyThree, Value = 123
Hope all that can help you.
Adobe help about looping:
The for..in loop iterates through the properties of an object, or the
elements of an array. For example, you can use a for..in loop to
iterate through the properties of a generic object (object properties
are not kept in any particular order, so properties may appear in a
seemingly random order)
Conclusion
If you want your keys to appear in order, you must use an Array (or a Vector):
var myArray:Array = [0, 1337, 123];
for (var i:String in myArray) {
trace(myArray[i]);
}
// output: 0, 1337, 123

What is the right way to compare as3 objects using hamcrest

I'm trying to compare two objects to see if they are the same using hamcrest for flex-unit, but when the object has sub objects, it just throws an error:
Error: Expected: (An array containing <[object Object]>
but: an array containing <[object Object]> was <[object Object]>
I want it to do an assertThat(..., hasProperties(...)); on the sub object.
Is there a way to get that or should i create a custom matcher?
EDIT
An example of the object structure i want to test:
var expected:Object = {
number:1.3,
array:[{
prop1:"val1", prop2:"val2"
}]
anObject:{
propA:1, propB:2
},
}
var objectUnderTest:Object = {
number:1.3,
array:[{
prop1:"val1", prop2:"val2"
}]
anObject:{
propA:1, propB:2
},
}
assertThat("should be the same", objectUnderTest, hasProperties(expected));
since the expected and objectUnderTest have the same structure, the test should pass, but is returning the error:
Error: Expected: (An array containing <[object Object]>
but: an array containing <[object Object]> was <[object Object]>
Also, if there is a way to compare two JSON strings will be fine too.
EDIT2
This is my final version after djib help:
package com
{
public function assertEqualsObjects(message:String, object1:Object, object2:Object):Boolean
{
// we have to run it both ways (1-2, 2-1)
return (compare(object1, object2, message + ": object") && compare(object2, object1, message + ": extra"));
}
}
import org.flexunit.asserts.fail;
function compare(object1:Object, object2:Object, parent:String):Boolean
{
var count:int = 0;
for (var s:String in object1)
{
count ++;
if (!object2.hasOwnProperty(s))
{
fail(parent + "." + s + " expected: " + object1[s] + " but was: undefined");
return false;
}
if (!compare(object1[s], object2[s], parent + "." + s))
{
fail(parent + "." + s + " expected: " + object1[s] + " but was: " + object2[s]);
return false;
}
}
if (count == 0 && object1 != object2) // if object has no properties, compare their actual values
{
fail(parent + " expected: " + object1 + " but was: " + object2);
return false;
}
return true;
}
I've put this code together. Recursion is the key ^^
// our two objects to compare ...
var obj1 = {
number:1.3,
array:[{prop1:"val1", prop2:"val2"}],
anObject:{propA:1, propB:2}
};
var obj2 = {
number:1.3,
array:[{prop1:"val1", prop2:"val2"}],
anObject:{propA:1, propB:2}
};
trace(isSame(obj1, obj2)); // -> true
function isSame(object1:Object, object2:Object):Boolean
{
// we have to run it both ways (1-2, 2-1)
return (compare(object1, object2) && compare(object2, object1));
}
function compare(object1:Object, object2:Object):Boolean
{
var count:int = 0;
for (var s:String in object1)
{
count ++;
if (object2[s] == undefined)
return false;
if (!compare(object1[s], object2[s]))
return false;
}
if (count == 0 && object1 != object2) // if object has no properties, compare their actual values
return false;
return true;
}

json not matching model

In EXTJS i will use a model and store for my grid. Now is the problem that sometimes the json will not match the model. There will be less information then in my model. When this happens EXTJS will not show any data in the grid. So i looked for a fix and found this:
Ext.define('App.Reader', {
extend: 'Ext.data.reader.Json',
extractData: function(root) {
var me = this,
values = [],
records = [],
Model = me.model,
i = 0,
length = root.length,
idProp = me.getIdProperty(),
node, id, record;
if (!root.length && Ext.isObject(root)) {
root = [root];
length = 1;
}
for (; i < length; i++) {
node = root[i];
values = me.extractValues(node);
id = me.getId(node);
record = new Model(values, id, node);
records.push(record);
if (me.implicitIncludes) {
me.readAssociated(record, node);
}
}
return records;
},
extractValues: function(data) {
var fields = this.getFields(),
i = 0,
length = fields.length,
output = {},
field, value;
for (; i < length; i++) {
field = fields[i];
value = this.extractorFunctions[i](data);
if(value === undefined)
{
Ext.iterate(fields, function(key, val) {
if (data[key] === undefined & i==val) {
console.log( "Model field <" + key.name + "> does not exist in data/node.");
value = "INVALID OR MISSING FIELD NAME";
var p = 0;
for(var prop in data) {
if(p==i){
if(data.hasOwnProperty(prop))console.log("Instead of <" + key.name + "> we have <" + prop + "> with value <" + data[prop]+ ">");
}
p++;
}
}
}, this);
}
output[field.name] = value;
}
return output;
}
});
var myReader = new App.Reader({
type:'json'
});
i found this online. But when i use this with EXTJS 4.1.1 there is an error in ext-all: TypeError: j is undefined.
Where should i look for the fix for this?
There's no need to do something complicated to solve this trivial problem. Read up on Ext.data.Model and Ext.data.Field, configure your Model properly and you're all set.