开发者

as3 evaluate string from json as object

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. 开发者_如何学Python 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);


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜