开发者

Casting JSON/XML in AS3

Having used FlashDevelop with a project, and switched to FDT/Eclipse. I am sorting through hundreds of warnings in my code.

Quite a few relate to XML and J开发者_开发技巧SON syntax this project is using. In the following, I'm not sure what to cast the XML containing data as so that it is recognised by the compiler

Example:

public function convertXMLToAssets(file:XML):void
{
    var data:XML = new XML(file.data);
    var id:int = data.item.id;
    //etc
}

//gives Warning: Could not resolve variable (may be a dynamic member) data at line 59 


file:String doesn't have a "data" property (String.data is not valid). Is it an URLLoader? in that case, change it to file:URLLoader, or whatever it is (Object maybe?). Also, I don't see the declaration of the "i" variable, is it a class member?


that's not a warning, that's a real error ... String does not have a property data, as Cay mentioned ... I wonder how you ever made this compile with FD in the first place ... did you compile with CS3 or with Flex SDK?

it should be something like

var data:XML = XML(file);
var id:int = data.item.id;/*if your xml looks something like 
          <%ROOT%>
                <item>
                    <id>%SOMEINT%</id>
                </item>
         </%ROOT%>*/

by the way, with JSON, using as3corelib, it'd be

var data:XML = JSON.decode(file);
var id:int = data.item.id;/*if your JSON looks something like
         { item : { id: %SOMEINT% } }

JSON and XML have extremely different semantics, and XMLNode, that you mentioned is ActionScript 2 Legacy, that you shouldnt use ... no offense, but I think you should look at either a JSON or XML/E4X tutorial, since you code and what you say, somehow leads me to believe, you didn't understand some fundamental things, such as the general process:

source string ---parsing/unmarshalling---> intermediary object tree ---traversal---> extracted data

neither can you operate on the source string directly, nor are the intermediary object trees freely exchangable, and thus traversal also depends on used data encoding format (you cannot traverse parsed JSON with E4X, but then again JSON is a semantically equivalent representation of ActionScript values)


Well since everything in E4X is either XML objects or strings, I highly doubt that data.item[i].id can be a number. If the ID is a float, use parseFloat(data.item[i].id) and if it's just an int use +data.item[i].id.

Edit: Is there more than one <item/>? If so, I think you want data..item[0].id. Here's some stuff I think you may want to try:

  • data..item[0].id
  • data..item[0].@id
  • data.id
  • data.@id


There's a trick I use to avoid these kinds of warnings.

Instead of using the notation data.item.id you can use data['item']['id']

Yes, it's a bit ugly, but with that notation you don't get the warnings.

Don't abuse that notation since that avoids any compile-time checking. Use it only in places where you are sure it's correct and can't get rid of the warning otherwise. For example, with XML data, as you show here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜