将JSON数据解析为AS3并在flash文本字段中显示

时间:2014-12-10 20:01:12

标签: json actionscript-3 flash

首先,我不是程序员,而是平面设计师,我自己很难解决这个问题。

我需要的是从此网址https://api.lottotech.com/api/v1/345/UpcommingDraws获取位于“lotteryId”:“3”中的 estimatedJackpotLC 并将其解析为动态文本Flash Pro CC中的字段。我正在使用AS3和Flash Player 11(我听说本机支持JSON解析?)。怎么做?我真的很感激一些帮助。

这是JSON数据:

{   "totalCount": 0,
"items": [{
    "lotteryId": 3,
    "drawId": 303,
    "estimatedJackpotLC": 60000000.0,
    "endsOn": "2014-12-10T22:00:00Z"
},
{
    "lotteryId": 2,
    "drawId": 326,
    "estimatedJackpotLC": 10000000.0,
    "endsOn": "2014-12-12T13:00:00Z"
},
{
    "lotteryId": 1,
    "drawId": 331,
    "estimatedJackpotLC": 31000000.0,
    "endsOn": "2014-12-12T14:00:00Z"
},
{
    "lotteryId": 4,
    "drawId": 336,
    "estimatedJackpotLC": 102000000.0,
    "endsOn": "2014-12-12T22:00:00Z"
},
{
    "lotteryId": 5,
    "drawId": 367,
    "estimatedJackpotLC": 14400000.0,
    "endsOn": "2014-12-11T15:00:00Z"
},
{
    "lotteryId": 7,
    "drawId": 387,
    "estimatedJackpotLC": 12200000.0,
    "endsOn": "2014-12-14T16:30:00Z"
}]}

提前致谢!

1 个答案:

答案 0 :(得分:3)

这是你想要做的事情的简单代码,包括评论:

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.text.TextField;
import flash.globalization.CurrencyFormatter;

// create currency formatter
var cf:CurrencyFormatter = new CurrencyFormatter(LocaleID.DEFAULT);
cf.groupingSeparator = " "; // set the grouping separator to space
cf.fractionalDigits = 0; // don't show any decimal places

// creating a text field, you probably already have one on your display list
var dynamicTextField:TextField = new TextField();
addChild(dynamicTextField);

// load the JSON data from the URL
var urlLoader:URLLoader = new URLLoader(new URLRequest("https://api.lottotech.com/api/v1/345/UpcommingDraws"));
urlLoader.addEventListener(Event.COMPLETE, onLoadComplete);

// handle the load completion
function onLoadComplete(e:Event):void 
{
    var result:Object = JSON.parse(e.target.data); // parse the JSON data
    var items:Array = result.items; // retrieve the items array

    // loop over all the items...
    for each(var item:Object in items)
    {
        // ... until you find the one you're looking for
        if(item.lotteryId == 3) 
        {
            // set the text, using the currency formatter
            dynamicTextField.text = cf.format(item.estimatedJackpotLC, true); // true tells it to use the currency symbol 
            break; // break out of the loop b/c you found the item you're looking for
        }
    }
}