as3 将 json 中的字符串评估为对象

发布于 2024-11-16 16:16:15 字数 430 浏览 3 评论 0原文

我有一个 json 对象,我不知道编译时的一些值,但我知道所有对象在运行时都有效。因此,在下面的示例中,第一个跟踪将输出“50”,我希望第二个跟踪输出“100”,即 someObject.someparam 的值,该值在运行时定义。这可能吗?谢谢

var plan:Object = { "testParam": 50, "testParam2": "someObject.someParam" }
var someObject:Object = {"someParam": 100}// this actually doesn't get defined until runtime  

trace ("testParam " + plan.testParam);
trace ("testParam2 " + someSortOfInterpreter(plan.testParam2);

I have a json object where I don't know some of the values at compile time, but I do know that all objects will be valid at runtime. So in the example below, the first trace will output "50" and I want the second trace to output "100", the value of someObject.someparam, which gets defined at runtime. Is this possible? Thanks

var plan:Object = { "testParam": 50, "testParam2": "someObject.someParam" }
var someObject:Object = {"someParam": 100}// this actually doesn't get defined until runtime  

trace ("testParam " + plan.testParam);
trace ("testParam2 " + someSortOfInterpreter(plan.testParam2);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白首有我共你 2024-11-23 16:16:15

对于我为什么使用“JSON 对象”,这没有多大意义。 JSON 是基于文本的表示法,稍后可以由您正在使用的特定编码语言进行解释。

因此,假设您的 JSON 字符串实际上是:

var jsonString:String = '{
    "testParam": 50,
    "testParam2": "someObject.someParam"
}';

您可以在编译时完全省略“testParam”属性,然后解析该字符串并在运行时设置该属性。

首先:

var jsonString:String = '{
    "testParam": 50
}';

then:

var plan:Object = JSON.decode (jsonString);
plan.testParam2 = someObject.testParam;

假设您使用 as3coreLib JSON 类来解码 json 字符串。

This doesn't make much sense to me as to why you are using a "JSON Object." JSON is text based notation that can later be interpreted by the specific coding language you are using.

So, assuming your JSON string is actually:

var jsonString:String = '{
    "testParam": 50,
    "testParam2": "someObject.someParam"
}';

You could just leave out the "testParam" property entirely, at compile time, then parse the string and set that property at runtime.

Start with:

var jsonString:String = '{
    "testParam": 50
}';

then:

var plan:Object = JSON.decode (jsonString);
plan.testParam2 = someObject.testParam;

This is assuming you're using the as3coreLib JSON class to decode the json string.

蓝天 2024-11-23 16:16:15

对象是动态的,您不必在运行时创建它。

var someObject:Object = { }; // Empty object with nothing defined in it

trace(someObject.someParam); // Traces out "undefined"

您还可以检查它是否已设置

if (someObject.someParam != undefined)

您可以随时设置它

someObject.someParam = 100;

,现在,设置后,它将正确跟踪

trace(someObject.someParam); // Traces out 100

这是您遇到的麻烦吗?如果没有,也许您可​​以向我们提供有关您的问题的更多信息。

Objects are dynamic, it doesn't have to exist for you to create it at runtime.

var someObject:Object = { }; // Empty object with nothing defined in it

trace(someObject.someParam); // Traces out "undefined"

You can also check to see if it has been set

if (someObject.someParam != undefined)

You can set it whenever you want

someObject.someParam = 100;

and now, after it has been set, it will trace correctly

trace(someObject.someParam); // Traces out 100

Is this what you are having troubles with? If not, maybe you could give us more info about your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文