I am able to pull JSON data from PHP into my Flex application just fine with the following bit of code:
我可以使用以下代码将JSON数据从PHP中提取到我的Flex应用程序中:
public function httpResult(event:ResultEvent):void {
var rawData:String = String(event.result);
trace(String(event.result)); //shows correct order
var as3Data:Object = JSON.decode(rawData);
for (var i:* in as3Data) {
trace(i + ": " + as3Data[i].unit_price); //shows incorrect order
}
}
When I trace the result, I see the information I am pulling in the correct order.
当我追踪结果时,我会看到正在按正确顺序提取的信息。
{"100":{"unit_price":"2.9567"},"400":{"unit_price":"1.0991"},"800":{"unit_price":"0.7926"},"1200":{"unit_price":"0.6911"}} {
But, once I JSON.decode the result, somehow it re-orders the content. And, puts the first item last.
但是,一旦我JSON.decode结果,它会以某种方式重新排序内容。并且,将第一项放在最后。
400: 1.0991, 800: 0.7926, 1200: 0.6911, 100: 2.9567
400:1.0991,800:0.7926,1200:0.6911,100:2.9567
Does anyone have any ideas on why it would be doing this? Or ideas on how I can re-order the Object myself?
有没有人对为什么会这样做有任何想法?或者我如何自己重新订购Object的想法?
2 个解决方案
#1
1
Objects in AS3 aren't ordered. JSON key-value pairs obviously do have an ordering (it's in the text!), but I don't think there's any guarantee it'll be kept when the JSON is either encoded or decoded.
AS3中的对象未订购。 JSON键值对显然有一个排序(它在文本中!),但我不认为在JSON编码或解码时会保留它。
If you've got specific ordering requirements, you should probably create a list with objects in it:
如果您有特定的订购要求,您应该创建一个包含对象的列表:
[
{"100":{"unit_price":"2.9567"}},
{"400":{"unit_price":"1.0991"}},
{"800":{"unit_price":"0.7926"}},
{"1200":{"unit_price":"0.6911"}}
]
#2
0
For the most part Andrew's answer is right on, because yeah objects in AS3 aren't ordered. However I wouldn't create a list of objects; rather I would create a list of keys used to index into the JSON object. That way it's easy to sort the list of keys.
在大多数情况下,安德鲁的回答是正确的,因为AS3中的对象不是有序的。但是我不会创建对象列表;相反,我会创建一个用于索引到JSON对象的键列表。这样就很容易对键列表进行排序。
The code for what I'm getting at is something like:
我得到的代码是这样的:
var as3Data:Object = JSON.decode(rawData);
var keys:Array = [];
for (var i:* in as3Data) {
keys.push(i);
}
keys.sort(); //don't know the correct sort off the top of my head, sorry
for (var key:* in keys) {
trace(key + ":" + as3Data[key].unit_price);
}
#1
1
Objects in AS3 aren't ordered. JSON key-value pairs obviously do have an ordering (it's in the text!), but I don't think there's any guarantee it'll be kept when the JSON is either encoded or decoded.
AS3中的对象未订购。 JSON键值对显然有一个排序(它在文本中!),但我不认为在JSON编码或解码时会保留它。
If you've got specific ordering requirements, you should probably create a list with objects in it:
如果您有特定的订购要求,您应该创建一个包含对象的列表:
[
{"100":{"unit_price":"2.9567"}},
{"400":{"unit_price":"1.0991"}},
{"800":{"unit_price":"0.7926"}},
{"1200":{"unit_price":"0.6911"}}
]
#2
0
For the most part Andrew's answer is right on, because yeah objects in AS3 aren't ordered. However I wouldn't create a list of objects; rather I would create a list of keys used to index into the JSON object. That way it's easy to sort the list of keys.
在大多数情况下,安德鲁的回答是正确的,因为AS3中的对象不是有序的。但是我不会创建对象列表;相反,我会创建一个用于索引到JSON对象的键列表。这样就很容易对键列表进行排序。
The code for what I'm getting at is something like:
我得到的代码是这样的:
var as3Data:Object = JSON.decode(rawData);
var keys:Array = [];
for (var i:* in as3Data) {
keys.push(i);
}
keys.sort(); //don't know the correct sort off the top of my head, sorry
for (var key:* in keys) {
trace(key + ":" + as3Data[key].unit_price);
}