dt1000
dt1000

Reputation: 3732

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. 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);

Upvotes: 2

Views: 2972

Answers (2)

Flassari
Flassari

Reputation: 1239

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.

Upvotes: 0

sapptime
sapptime

Reputation: 430

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.

Upvotes: 1

Related Questions