Hello, everyone.
I'm given an Array, it is not in format {abc:1, qwe:'ghj'}. The array looks like this:
new Array(["SMTH", 1, 2, 3], ["ANTH", 0, 0, 0, [4, 5, 6]], ["ZZZ", 8])
As you can see, it has only square brackets, and even in other square brackets.
The task is:
1) Convert this Array to String, because server only saves strings. Save it.
2) Load saved String and convert it to Array.
So, a usual task for the game.
Please, tell me, in what Language they may be converted, what should I do.
1) I've tried to convert (1) Array to String, so that square brackets remain, even in another [], but AS3 removes them. Well, I wrote my own function. Now the result (1) looks exactly like you saw above in bold letters.
2) But another problem exists: how to convert that string to Array back...
Array.match() says there is no array, seems like it doesn't notice square brackets at all.
I've tried split, IndexOf and others, but got stuck in those [[]]
Simple myString as Array also doesn't work.
I suppose, I'm doing it all wrong. So i've tried converting Array to JSON. It turned out to be too much odd symbols, it was saved, but i have an limitation of symbols count, so JSON is not the answer.
there is probably a cleaner way to do thisn using recursion but this will do what you ask for i think
you could find solutions buy doign a google search for serialize
package
{
import flash.display.Sprite;
public class Main5 extends Sprite
{
public function Main5()
{
var array:Array = new Array(["SMTH", 1, 2, 3], ["ANTH", 0, 0, 0, [4, 5, 6]], ["ZZZ", 8]);
trace("array: " + array);
var string:String = convertToString(array);
trace("string: " + string);
var array2:Array = convertToArray(string);
trace("array2: " + array2);
}
private function convertToString(array:Array):String
{
var output:String = "";
for (var i:int = 0; i < array.length; i++)
{
var level1Items:Array = array[i] as Array;
for (var j:int = 0; j < level1Items.length; j++)
{
var level1Item:* = level1Items[j];
if (level1Item is Array)
{
for (var k:int = 0; k < level1Item.length; k++)
{
output += level1Item[k];
if (k < level1Item.length -1)
{
output += ","
}
}
}
else
{
output += level1Item;
if (j < level1Items.length - 1)
{
output += "|";
}
}
}
if (i < array.length - 1)
{
output += "&";
}
}
return output;
}
private function convertToArray(string:String):Array
{
var output:Array = new Array();
var level1Items:Array = string.split("&");
for (var i:int = 0; i < level1Items.length; i++)
{
output[i] = new Array();
var level1Item:String = level1Items[i];
var level2Items:Array = level1Item.split("|");
for (var j:int = 0; j < level2Items.length; j++)
{
var level2Item:String = level2Items[j];
if (level2Item.indexOf(",") > -1)
{
output[i][j] = new Array();
var level3Items:Array = level2Item.split(",");
for (var k:int = 0; k < level3Items.length; k++)
{
output[i][j][k] = level3Items[k];
}
}
else
{
output[i][j] = level2Item;
}
}
}
return output;
}
}
}
Wow, seems like your function convertToString is almost copy of mine
I've also tried to separate array Items not only by square brackets.
Now I'm gonna try using convertToArray, thanks beforehand!!! I'll write about results.
PS.Only I have this forum viewing strangely? I remember that two weeks ago it looked more pretty...
Hm...
The String, which is going to server, looks like that:
FENCE_004|1|2|3&FENCE_004|0|0|0|4,5,6&ZZZ|8
So far, it is all quite clear.
The Array, created after loading that String looks like this:
FENCE_004,1,2,3 amp;FENCE_004,0,0,0,4,5,6 amp;ZZZ,8
Where spaces are new line.
I don't understand, why code is not removing & and turning it to amp; in the beginning of next item...
________________________________________
Found why:
Loaded String looks like
FENCE_004|1|2|3&FENCE_004|0|0|0|4,5,6&ZZZ|8
________________________________________
Why server translated that symbol to another one? Are there any, which wouldn't?
Of course, I
could replace all the instances of & with & using String.replace()
but, it is better not to save & to server at all (limited space), so I've used tilda, not &, when converting to String. It was saved and loaded as is, as tilda... But I think it's not a kind of special symbol.
Unfortunately, charCodeAt returns numbers, so there would be no way to understand, where was Number or charCode
As far as I've understood, server is 'talking' on HTML, so it has symbols, which would be 'translated' before saving them, because it also uses the same when 'talking'. The example is & translated to &
What special symbols (not used normally by user) can be used in HTML not being 'translated'?
As I've mentioned before, I have limited space, and JSON returns String full of odd symbols, which I have to save to server.
to esdebon
As I've mentioned before, charCodeAt() rerturns numbers. We have
FENCE_004|1|2|3&FENCE_004|0|0|0|4,5,6&ZZZ|8
If I have to make symbol & to Unicode, the resulting String will look like this:
FENCE_004|1|2|338FENCE_004|0|0|0|4,5,638ZZZ|8
There is no way to understand, where to start using fromCharCode(), because 3& turned to be 338
I'm just asking for advice: what symbols are not 'translated' by HTML (because & is)?
North America
Europe, Middle East and Africa
Asia Pacific