【问题标题】:Parse JSON data to AS3 and show it in flash text field将 JSON 数据解析为 AS3 并在 Flash 文本字段中显示
【发布时间】:2014-12-11 05:39:55
【问题描述】:

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

我需要的是从该 URL https://api.lottotech.com/api/v1/345/UpcommingDraws 获取 "lotteryId":"3" 中的 estimatedJackpotLC 并将其解析为闪光临 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"
}]}

提前致谢!

【问题讨论】:

  • 最好将您尝试解析的内容的示例直接粘贴到问题中——我无法访问该 URL,而且看起来也有点可疑。
  • 您好,我已经用其中的 json 数据编辑了我的问题。

标签: json actionscript-3 flash


【解决方案1】:

这是您要执行的操作的基本代码,包括 cmets:

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
        }
    }
}

【讨论】:

  • 感谢您的快速回复,您是最棒的,Marcela!我已设法将结果放入我创建的文本字段中,因此我可以编辑颜色和动画等内容,但是否可以将结果格式化为类似于“60 000 000 美元”而不是“60000000”?再次感谢,您已经帮了我很多忙!
  • 我已经更新了代码来告诉你如何使用AS3的原生CurrencyFormatter
  • 再次感谢马塞拉。该代码对我来说完美无缺!
  • 由于某种原因,它可以在 Flash 预览中运行(ctrl+enter),但是当我打开 .swf 文件时它没有显示任何内容。我尝试将其上传到远程服务器并打开它,仍然一样。尝试使用 HTML 发布它,但不起作用。用代码尝试了一些东西,不,不工作。我什至只用代码创建了一个空白文件,是的,它是一样的——只在预览中工作。非常令人沮丧。看起来像一些安全措施什么的。有什么想法吗?
  • 对垃圾邮件感到抱歉,但如果我创建 new URLRequest("UpcommingDraws.txt") 并创建一个本地 .txt 文件,它工作正常。但远程加载 JSON 时不会。
猜你喜欢
  • 1970-01-01
  • 2016-12-21
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多