Anyone an idea how I can get the values of the “attribute_values”? They are dynamically. In the first example I have to read the value of “1”(=“240 min”) and in the second example I have to read the value of “122”(=tgu)
I tried a lot: Using maps, parsing the value of “attribute_values” into a new json-object and read it with object.122 and many more… but nothing worked for me.
Or you can use the json2object library if you want to avoid Dynamic. It supports deserialization into a Haxe Map, among other things.
class Main {
public static function main() {
var parser = new json2object.JsonParser<Data>();
var obj = parser.fromJson('{ "1": "240 min" }', "data.json");
trace(obj[1]);
}
}
typedef Data = Map<Int, String>;
No matter what programming language you’re talking about, every language has one-or-more “JSON magic encoder/decoder rings” that are known to work correctly, and you should always use them blindly.
Treat the JSON string as a “black box” and hand it to the magic decoder-ring to be turned into a data structure. Be prepared that the decoder-ring might throw an exception if it is handed ill-formed JSON … never assume that “the other guy” knew what he was doing, and recover graciously if he didn’t. (Or, if he’s trying to cause trouble…)
When ready to transmit, use the magic encoder-ring and send the exact string that it gives you.
Exactly the same principles apply, of course, to XML and SOAP. There are many subtle nuances to be dealt with, and somebody else has already figured them out for you.