So far using FlexJS SDK HTTPService API coming very strangely behaving to me.
Maybe that is because I don’t know many things which are ‘supposed to’ act
‘as is’ when FlexJS HTML output. I’m trying to brief in my experience so can
contributors to the SDK can justify if something is wrong or can guide me
ahead.
As I’ve seen in varied demo projects those supplied within FlexJS SDK
folder, calling a HTTPService and parsing it’s JSON data consists of two
classes - JSONInputParser and JSONItemConverter - user can therefore extend
these two classes further to chunk the JSON strings by subStr, indexOf etc.
and populate AS objects. Since data structure in our organisation often
quite complex these string parsing process may not be that intuitive for us.
Investigating ahead, I noticed that some demo projects shown JSON parsing in
following ways too, where the parsing uses a new instance of JSONInputParser
class (no extend) and an extended JSONItemConverter class:
/service = new HTTPService();
collection = new LazyCollection;
collection.inputParser = new JSONInputParser();
collection.itemConverter = new StockDataJSONItemConverter();/
I took sometime to debug such projects in Flash Builder, and I found the
JSONInputParser did actually what I wanted - creating generic AS objects out
of JSON string without letting me write string chunking scripts inside an
extended JSONInputParser class! That was great! I found parsed generic AS
objects in my data service call ‘complete’ event - and therefore I can write
my own parser logic to convert those generic objects to my valueobjects.
Thus my code became like this:
/public function DataAgent(url:String, success:Function, error:Function,
sendObject:String=null)
{
httpS = new HTTPService();
httpS.url = url;
collection = new LazyCollection;
collection.inputParser = new JSONInputParser();
httpS.addBead(collection);
httpS.addEventListener("complete", onCompleteEvent);
httpS.addEventListener("ioError", onIOError);
httpS.send();
}
private function onCompleteEvent(event:Event):void
{
trace(event.target.json); // I found property ‘json’ holding parsed
generic
AS objects by JSONInputParser class instance
// write my parsing logic here
MyModel(app.model).dataList = new ArrayList();
var tmpDocuments:Array = event.target.json.document;
for (var i:int = 1; i < tmpDocuments.length; i++)
{
var tmpChain:AgentChain = new AgentChain();
var fields:Array = tmpDocuments[i];
for each (var j:Object in fields)
{
if (j["@name"] == "ActionID") tmpChain.ActionID =
j.text;
if (j["@name"] == "ActionDescription")
tmpChain.ActionDescription =
j.text;
if (j["@name"] == "ActionFinished")
tmpChain.ActionFinished = (j.text ==
"true") ? true : false;
...
}
MyModel(app.model).dataList.addItem(tmpChain);
}
}/
Voila! This worked great when output either as SWF or HTML! I tested the
above in MacOS (Yosemite 10.10.5) Safari. Things starts breaking when we
started to test this in other browsers and in Windows and OUTPUT as HTML.
We noticed when ran in other browsers than Safari in MacOS or anywhere in
Windows, ‘event.target.json’ or ‘event.target.data’ is coming blank or no
such property. This is very weird why this happening when a particular
browser in MacOS working well. We found almost no way we can able to get the
said properties inside ‘target’ of a ‘complete’ event. Investigating this
further, Apache provided demo projects anyway using JSONInputConverter class
which I didn’t. So I moved to use an instance of that class and see what is
happening.
Following are my updated HTTPService call commands:
/public function DataAgent(url:String, success:Function, error:Function,
sendObject:String=null)
{
httpS.url = url;
collection = new
collection.inputParser = new JSONInputParser();
collection.itemConverter = new StockDataJSONItemConverter();
httpS.addBead(collection);
httpS.addEventListener("complete", onCompleteEvent);
httpS.addEventListener("ioError", onIOError);
httpS.send();
}
private function onCompleteEvent(event:Event):void
{
collection.getItemAt(0); // <— calls JSONItemConverter instances
}/
Inside StockDataJSONItemConverter custom class:
/override public function convertItem(data:String):Object
{
var obj:Object = super.convertItem(data);
…
}/
Before testing the above I expected that JSONItemConverter’s convertItem()
method will receive only one long JSON data string (maybe I’m wrong) and
therefore I can able to convert the string in generic AS objects by
super.convertItem(data); method. When ran, I debugged that collection length
in onCompleteEvent() are 35. Calling getItemAt(0) having a JSON string
inside JSONItemConverter which is a part somewhere in middle! Isn’t that
JSON string part supposed to be from beginning of main JSON when I called
*/getItemAt(0)/*?
Following part I got when getItemAt(0):
/"_ga=xxx","_jsuid=xxx","LtpaToken=xxxx"],"lookup":"P7.2.7G6GXL","document":[{"@name":"CustID_AccessList","textlist":["A55555:Billing","A55555:Technical","A55555:DesktopUser","A55605:Technical","A55C03:Technical","A55C03:UserAdmin","A55C03:Billing","A55D78:Technical","A55DE9:Technical"]/
The whole JSON is hosted at here:
http://www.gadhavitechnologies.com/project/Flex/testData.json
Now my problem is converting these irregular JSON string by JSONInputParser
does not makes sense to me. If I would have a complete JSON string inside
JSONItemConvert and test it’s super.convertItem(data) in HTML output - that
might make sense to me, and we could able to get specific objects in AS. I
would like to note again that in many other cases the data structures are
quite complex, so write a brute logic by string chunking manually could
drive us in nightmares. How can we get generic AS objects out of JSON data
and inside any browser platform across Windows or MacOS?
Thanks.
--
View this message in context:
http://apache-flex-users.2333346.n4.nabble.com/FlexJS-HTTPService-for-XML-parsing-tp12094p12137.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.