热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将JSON转换为自定义ActionScript对象?-CastJSONasacustomActionScriptobject?

HelloIwaswonderingifitispossibletocastmyJSONstringasacustomobject?您好我想知道是否可以将我的JSO

Hello I was wondering if it is possible to cast my JSON string as a custom object?

您好我想知道是否可以将我的JSON字符串转换为自定义对象?

basically :

基本上:

var customObject:CustomObject = JSON.decode(evt.result as String) as CustomObject;

Regards Adlertz

关心阿德尔茨

5 个解决方案

#1


4  

In AS3, you cannot cast a dynamic object to a custom class using as or CustomClass(customObject).

在AS3中,您无法使用as或CustomClass(customObject)将动态对象强制转换为自定义类。

You can, however, use some simple tricks as a workaround. For example, you could declare a constructor for your custom class accepting an Object and initializing it's members with the object properties.

但是,您可以使用一些简单的技巧作为解决方法。例如,您可以为自定义类声明一个接受Object的构造函数,并使用对象属性初始化它的成员。

You would then use:

然后你会使用:

var customObject:CustomClass = new CustomClass(JSON.decode(evt.result as String));

PS. Regarding the comments, this is not true for every language... I guess that makes it actionscript specific.

PS。关于评论,对于每种语言都不是这样......我想这会使它成为特定的动作。

#2


3  

Per se, this is not possible. And that has nothing to do with ActionScript. In most other languages you have the same problem, since on the left side you have an anonymous object, if the language supports any such thing, or a hash. Anyway. There are different solutions, this would be one, that can handle a few things:

本身,这是不可能的。这与ActionScript无关。在大多数其他语言中,你有同样的问题,因为在左侧你有一个匿名对象,如果语言支持任何这样的东西,或哈希。无论如何。有不同的解决方案,这可以是一个,可以处理一些事情:

package  {
    public class ObjectUtils {
        public static function createInstance(constructor:Class):* {
            var ret:*;
            switch (describeType(to).factory.constructor.parameter.(@optiOnal== "false").length()) {
                case 0: ret = new to(); break;
                case 1: ret = new to(null); break;
                case 2: ret = new to(null, null); break;
                case 3: ret = new to(null, null, null); break;
                case 4: ret = new to(null, null, null, null); break;
                case 5: ret = new to(null, null, null, null, null); break;
                case 6: ret = new to(null, null, null, null, null, null); break;
                case 7: ret = new to(null, null, null, null, null, null, null); break;
                case 8: ret = new to(null, null, null, null, null, null, null, null); break;
                case 9: ret = new to(null, null, null, null, null, null, null, null, null); break;          
                default: throw new Error("no implementation for instantiating classes that require more than 9 constructor arguments");
            }
            return ret;
        }
        public static function castAnonymous(obj:Object, to:Class):* {
            var ret = createInstance(obj);
            for (var name:String in obj)
                try {
                    ret[name] = obj[name];
                }
                catch (e:Error) {
                    throw new Error("error trying to assign value " + obj[name] + " to property " + name + " on " + ret + ". reason: " + e);
                }
            return ret;
        }               
    }   
}

restrictions:

限制:

  1. will fail if your class panics, if it is spammed with nulls upon construction, or the constructor needs more than 9 arguments
  2. 如果您的类恐慌,如果在构造时使用空值发送垃圾邮件,或者构造函数需要超过9个参数,则会失败
  3. does not, and also cannot recurse, so it may simply assign anonymous objects or arrays to the returned instance's properties
  4. 不会,也不能递归,所以它可以简单地将匿名对象或数组分配给返回的实例的属性

hope it helps anyway ;)

希望它无论如何都有帮助;)

greetz

格尔茨

back2dos

back2dos

#3


2  

Actually - you can kinda hack around this restriction using the build in parser and overriding the JSON.parse method and taking advantage of the scope of the anonymous function to access an object in the parent function.

实际上 - 您可以使用内置解析器并覆盖JSON.parse方法并利用匿名函数的范围访问父函数中的对象来解决此限制。

For instance - check out this code snippet

例如 - 查看此代码段

public dynamic class MutatorData extends Object
{
    public var DisplayName:String;
    public var PropertyName:String;
}

public function parseData( data:String )
{
    var mutator:MutatorData = new MutatorData();

    JSON.parse( data,
        function (k, v) {
        mutator[k] = v;
        });
}

With the code sample as is - the MutatorData class has to be declared dynamic, which ALMOST defeats the purpose of creating a class for it. You wont be able to prevent other coders from adding properties to it, spelling mistakes in your JSON string will become properties on the object. Evenso, you'll get some RTTI and code hinting in the IDE - which can help prevent coder error.

使用代码示例 - MutatorData类必须声明为动态,ALMOST违背了为其创建类的目的。您无法阻止其他编码器向其添加属性,JSON字符串中的拼写错误将成为对象的属性。 Evenso,您将在IDE中获得一些RTTI和代码提示 - 这有助于防止编码器错误。

But it would be a simple matter to write a custom version of the parse method that would work with a final (non-dynamic) class.

但是编写一个可以与最终(非动态)类一起使用的parse方法的自定义版本将是一件简单的事情。

On the project I am currently working on - memory and runtime performance are critical, and we prefer using custom parsers for these reasons:

在我目前正在研究的项目上 - 内存和运行时性能至关重要,我们更喜欢使用自定义解析器,原因如下:

1) It allows us to alter the strictly typed object at the get-go and forgo copying the data into a temporary generic object.

1)它允许我们在一开始就改变严格类型的对象,并放弃将数据复制到临时通用对象中。

2) We can build the validator right into the parser, throwing run-time errors as necessary to notify the program we have receive incorrect JSON data.

2)我们可以在验证器中构建验证器,根据需要抛出运行时错误,以通知程序我们收到错误的JSON数据。

#4


2  

there is a way

有一种方法

as3-vanilla lib on github, very good : )

github上的as3-vanilla lib,非常好:)

https://github.com/jonnyreeves/as3-vanilla

https://github.com/jonnyreeves/as3-vanilla

#5


0  

You cannot cast custom objects from dynamic objects. But you can extend the JSON-Decoder from as3corelib. I did that for exactly this reason. When I'm decoding a json-String I pass the class name of the encoded object. With a little use of reflection you get a strong typed custom object back. Of course you need to know the class name of the encoded object before decoding...

您无法从动态对象强制转换自定义对象。但是你可以从as3corelib扩展JSON-Decoder。我之所以这么做是出于这个原因。当我解码json-String时,我传递了编码对象的类名。通过少量使用反射,您可以获得强大的自定义对象。当然你需要在解码之前知道编码对象的类名...


推荐阅读
author-avatar
mobiledu2502860957
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有