'JSON: Providing Constant Values
Though I'm using iOS, I don't think that's relevant here.
I need to be able to use a constant in two of the values for JSON. Reason: We're using different servers for Development and Production, and can't have the development data mixed with production Data. At build time we'll provide values based on the environment settings (development / production).
Thus, how can I use a constant value for "rsids" and server" in the JSON below:
{
"version" : "1.0",
"analytics" : {
"rsids" : "//I would like to put a Constant Here",
"server" : "",
"charset" : "UTF-8",
"ssl" : false,
"offlineEnabled" : true,
"lifecycleTimeout" : 300,
"privacyDefault" : "optedin",
"poi" : []
},
"target" : {
"clientCode" : "",
"timeout" : 5
},
"audienceManager" : {
"server" : "//I would like to put a Constant Here"
}
}
I would like to be able to do something like this:
NSStirng const *kServerURL = @"www.google.com"
"audienceManager" : {
"server" : kServerUrl
}
UPDATE
There is no Dictionary. I'm using the iOS SDK by Adobe for Site Catalyst. They have a JSON file that I have to edit and provide RSID and Server values. The JSON that you see in the question is the entire file.
The SDK has a library file (.a) also. Here are the docs: http://microsite.omniture.com/t2/help/en_US/mobile/ios/index.html#ADBMobile_Class_and_Method_Reference
Solution 1:[1]
If you need to change the value of the constant at build time you should be using #if/#ifdef and friends to do so.
#ifdef DEBUG
NSString const *kServerURL = @"www.google.com"
#else
NSString const *kServerURL = @"www.bing.com"
#endif
Solution 2:[2]
You'll have to create multiple files then. You can't have a JSON file be dynamic.
When creating your files from code, you can use constants. At the top of your .m file before the @implementation portion, defined them on multiple lines. One for development, one for QA, and one for production. Comment out the two you don't want and leave the one you do want. When using it, use the format.
"server":[NSString stringWithFormat:@"\"%@\"",kServerUrl];
It is often a good idea to have a dummy login that can safely communicate with your servers. Such a login would have an example set of data to be read, and any uploads would be ignored. This is handy for testing between the different environments, including production, and allows any apps submitted to the App Store to be fully tested with a valid login.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | memmons |
| Solution 2 | Aaron Bratcher |
