在flex4.7中获取js传递过来的json并解析

本文详细介绍了如何在HTML页面中通过JS传递JSON参数至Flex应用,并在Flex端正确解析这些参数的过程。重点在于参数传递的格式及Flex端的解析逻辑。

看上去简简单单的问题,前前后后用了我一两天的时间,中间一些本来调通的问题,因为没有保存又重新Debug了很长时间。

首先,我要实现的目的是从js中传递json参数到flex中,然后在flex要对传递过来的参数进行解析。

在html页面中,我们需要这样写

<script type="text/javascript">
 var swfVersionStr = "${version_major}.${version_minor}.${version_revision}";//swf要求的Flash版本号
 var xiSwfUrlStr = "${expressInstallSwf}";//快捷安装的swf
 //参数传递,注意格式为{params1:"",params2:""}出现其他格式时会被解析成Object,然后怎么都得不到想要的结果,
 //如果有人可以从传递的Object得到要的值,希望可以给小弟指点一下
 var flashvars = {num:"1",json:"[{'summoney':'666310.09','sumcount':'652','sumdate':'2014-08-05'},{'summoney':'0.19','sumcount':'9','sumdate':'2014-08-08'}]" };
 //当前swf在网页中显示的样式,包括背景等
 var params = {};
 params.quality = "high";
 params.bgcolor = "${bgcolor}";
 params.allowscriptaccess = "sameDomain";
 params.allowfullscreen = "true";
 //当前swf的属性,包括id,name,对齐方式等
 var attributes = {};
 attributes.id = "${application}";
 attributes.name = "${application}";
 attributes.align = "middle";
 //调用swfobject.js中的swf加载的方法,参数列表
 //(1、swf路径;2、在网页中显示的组件id-必须存在否则不显示;
 //3、swf所占的宽度,单位默认为像素px,直接给定数值即可;4、swf所占的高度,单位同宽度;
 //5、swf要求的Flash版本号;6、快捷安装显示的swf;7、传递给Flash的参数;8、swf的样式;
 //9、swf的基本属性)
 swfobject.embedSWF(
  "${swf}.swf", "flashContent", 
  "${width}", "${height}", 
  swfVersionStr, xiSwfUrlStr, 
  flashvars, params, attributes);
 swfobject.createCSS("#flashContent", "display:block;text-align:left;");
</script>
<div id="flashContent"><!--当没有安装Flashplayer或者版本过低时,到官网下载安装-->
 <p>
  To view this page ensure that Adobe Flash Player version 
  ${version_major}.${version_minor}.${version_revision} or greater is installed. 
 </p>
 <script type="text/javascript"> 
  var pageHost = ((document.location.protocol == "https:") ? "https://" : "http://"); 
  document.write("<a href='http://www.adobe.com/go/getflashplayer'><img src='" 
      + pageHost + "www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash player' /></a>" ); 
 </script> 
</div>



在网页中,主要注意的是参数的格式,因为这是本文的重点。
在flex中(我使用的是4.7),也就是我们用于生成swf的.mxml文件中,主要代码如下

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
   xmlns:s="library://ns.adobe.com/flex/spark" 
   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="100%" minHeight="100%">
   <fx:Script>
  <![CDATA[
   public var arrList:Array;
   private function onCreationComplete() : void {
    var parameters : Object = this.parameters;//获得参数(还可以用FlexGlobals.topLevelApplication.parameters),旧版本用Application.application.parameters获取
    var str:String = parameters.json;//参数列表中json参数
    var regE:RegExp=new RegExp("'", "g");//需要将"\'"转换为"\""否则会提示json串错误
    var array:Array=(JSON.parse(str.replace(regE,'"')) as Array);
    var para:String = "接收数据:\nnum="+parameters.num+";\njson=";
    for each(var object:Object in array){
     para += "日期:"+object["sumdate"]+";金额:";
     para += object["summoney"]+";笔数:";
     para += object["sumcount"]+";";
    }
    lblResult.text = para;//在label中显示数据
   }
  ]]>
 </fx:Script>
   <s:HGroup>
  <s:Label id="lblResult"/>
 </s:HGroup>
</s:Application>

上面的代码在Flash Builder中编辑后运行,运行结果:

以下代码中描述页面跳转时传输了哪些数据,作用是什么<view class=“location” @click=“navigateToCitySelect”> 广州从化 <view class=“search-box” @click=“navigateToSearch”> 搜索商家、美食… <view class="content" :style="{ marginTop: (statusBarHeight + 50) + 'px' }"> <swiper class="swiper" indicator-dots autoplay circular> <swiper-item v-for="(item, index) in banners" :key="index"> <image :src="item.image" mode="aspectFill" class="swiper-image"></image> </swiper-item> </swiper> <view class="coupon-banner" @click="navigateToCoupon"> <uni-icons type="notification-filled" size="24" color="#fff"></uni-icons> <view class="coupon-text"> <text class="coupon-title">新用户专享红包</text> <text class="coupon-desc">最高立减20元,点击立即领取</text> </view> <uni-icons type="arrowright" size="20" color="#fff"></uni-icons> </view> <view class="category-container"> <scroll-view scroll-x class="category-scroll"> <view v-for="(category, index) in categories" :key="index" class="category-item" @click="navigateToCategory(category.id)" > <view class="category-icon" :style="{ backgroundColor: category.color }"> <uni-icons :type="category.icon" size="30" color="#fff"></uni-icons> </view> <text class="category-name">{{ category.name }}</text> </view> </scroll-view> </view> <view class="section-title"> <text>推荐商家</text> <text class="more-link" @click="navigateToAllStores">查看更多 ></text> </view> <view class="store-list"> <view v-for="(store, index) in stores" :key="index" class="store-item" @click="navigateToStore(store.id)" > <image class="store-image" :src="store.image" mode="aspectFill"></image> <view class="store-info"> <view class="store-name"> <text>{{ store.name }}</text> <text class="min-price">¥{{ store.minPrice }}起送</text> </view> <view class="store-tags"> <text class="tag" v-for="(tag, i) in store.tags" :key="i">{{ tag }}</text> </view> <view class="store-detail"> <view> <uni-rate :value="store.rating" size="14" disabled></uni-rate> <text class="rating">{{ store.rating }}</text> <text>月售{{ store.monthlySales }}单</text> </view> <text>{{ store.distance }} | {{ store.deliveryTime }}分钟</text> </view> </view> </view> </view> </view> </view> </template> <script> export default { data() { return { statusBarHeight: 0, banners: [ { image: '/static/l1.jpg', }, { image: '/static/l2.jpg',}, { image: '/static/s.jpg', } ], categories: [ { id: 1, name: '美食', icon: 'shop', color: '#FF5A5F' }, { id: 2, name: '超市', icon: 'cart', color: '#0085FF' }, { id: 3, name: '甜品', icon: 'star', color: '#FF5A5F' }, { id: 4, name: '早餐', icon: 'circle', color: '#FFAA00' }, { id: 5, name: '火锅', icon: 'fire', color: '#FF5A5F' }, { id: 6, name: '轻食', icon: 'heart', color: '#34C759' } ], stores: [ { id: 1, name: '肯德基宅急送', image: '/static/ks.jpg', minPrice: 20, tags: ['品牌', '满减', '折扣'], rating: 4.8, monthlySales: 3245, distance: '1.2km', deliveryTime: 30 }, { id: 2, name: '麦当劳麦乐送', image: '/static/ms.jpeg', minPrice: 25, tags: ['品牌', '优惠券', '折扣'], rating: 4.7, monthlySales: 2987, distance: '0.8km', deliveryTime: 25 }, { id: 3, name: '必胜客宅急送', image: '/static/bs.png', minPrice: 50, tags: ['品牌', '新品', '满减'], rating: 4.6, monthlySales: 1876, distance: '1.5km', deliveryTime: 35 } ] } }, onLoad() { const systemInfo = uni.getSystemInfoSync(); this.statusBarHeight = systemInfo.statusBarHeight; }, methods: { navigateToSearch() { uni.showToast({ title: '跳转到搜索页面', icon: 'none' }) uni.navigateTo({ url: '/pages/category/search' }) }, navigateToCitySelect() { uni.showToast({ title: '选择城市', icon: 'none' }) }, navigateToProfile() { uni.switchTab({ url: '/pages/profile/profile' }) }, navigateToCoupon() { uni.showToast({ title: '跳转到优惠券页面', icon: 'none' }) uni.navigateTo({ url: '/pages/category/discount' }) }, navigateToCategory(id) { const categoryPages = { 1: '/pages/category/food', // 2: '/pages/category/supermarket', // 3: '/pages/category/dessert', // 4: '/pages/category/breakfast', // 5: '/pages/category/hotpot', // 6: '/pages/category/lightfood' }; if (categoryPages[id]) { uni.navigateTo({ url: categoryPages[id] }); } else { uni.showToast({ title: '页面正在开发中', icon: 'none' }); } }, navigateToAllStores() { uni.navigateTo({ url:'/pages/category/allstores' }) } } } </script> <style> page { background-color: #f5f5f5; color: #333; font-size: 28rpx; line-height: 1.5; } .container { background-color: #fff; min-height: 100vh; padding-bottom: 120rpx; position: relative; } .custom-navbar { position: fixed; top: 0; left: 0; right: 0; z-index: 100; background: linear-gradient(90deg, #0085ff, #0af); } .nav-content { display: flex; align-items: center; justify-content: space-between; padding: 10rpx 30rpx; height: 90rpx; } .location { display: flex; align-items: center; font-size: 32rpx; font-weight: bold; color: white; } .city-name { margin: 0 8rpx; max-width: 150rpx; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .search-box { flex: 1; margin: 0 30rpx; background: rgba(255, 255, 255, 0.8); border-radius: 50rpx; height: 70rpx; display: flex; align-items: center; padding: 0 25rpx; color: #666; } .search-box text { margin-left: 10rpx; font-size: 28rpx; } .content { padding: 20rpx; } .swiper { height: 320rpx; border-radius: 20rpx; overflow: hidden; box-shadow: 0 10rpx 20rpx rgba(0, 0, 0, 0.1); } .swiper-image { width: 100%; height: 100%; } .coupon-banner { margin: 30rpx 0; background: linear-gradient(90deg, #ff5e3a, #ff9500); border-radius: 16rpx; padding: 20rpx 25rpx; color: white; display: flex; align-items: center; box-shadow: 0 6rpx 12rpx rgba(255, 94, 58, 0.2); } .coupon-text { flex: 1; display: flex; flex-direction: column; margin-left: 20rpx; } .coupon-title { font-size: 32rpx; font-weight: bold; margin-bottom: 8rpx; } .coupon-desc { font-size: 24rpx; opacity: 0.9; } .category-container { background: #fff; padding: 20rpx 0 30rpx; border-radius: 20rpx; margin-bottom: 30rpx; box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.05); } .category-scroll { white-space: nowrap; width: 100%; padding: 0 10rpx; } .category-item { display: inline-flex; flex-direction: column; align-items: center; width: 150rpx; margin: 0 10rpx; } .category-icon { width: 100rpx; height: 100rpx; border-radius: 50%; display: flex; align-items: center; justify-content: center; margin-bottom: 15rpx; } .category-name { font-size: 26rpx; color: #666; white-space: nowrap; text-align: center; } .section-title { padding: 20rpx 0; font-size: 36rpx; font-weight: bold; display: flex; justify-content: space-between; align-items: center; } .more-link { font-size: 26rpx; color: #999; font-weight: normal; } .store-list { padding-bottom: 30rpx; } .store-item { background: #fff; border-radius: 20rpx; overflow: hidden; margin-bottom: 30rpx; box-shadow: 0 10rpx 20rpx rgba(0, 0, 0, 0.08); transition: transform 0.3s ease; } .store-item:active { transform: scale(0.98); } .store-image { width: 100%; height: 300rpx; } .store-info { padding: 20rpx; } .store-name { font-size: 32rpx; font-weight: bold; margin-bottom: 10rpx; display: flex; justify-content: space-between; } .min-price { color: #FF5A5F; font-weight: bold; } .store-tags { display: flex; margin-bottom: 15rpx; flex-wrap: wrap; } .tag { font-size: 22rpx; padding: 4rpx 12rpx; border-radius: 6rpx; margin-right: 10rpx; margin-bottom: 8rpx; background: #fff0f0; color: #ff5a5f; } .store-detail { display: flex; justify-content: space-between; align-items: center; font-size: 26rpx; color: #666; padding-top: 15rpx; border-top: 1rpx dashed #eee; } .rating { color: #ffaa00; font-weight: bold; margin: 0 10rpx; } .tabbar { position: fixed; bottom: 0; left: 0; right: 0; height: 100rpx; background: white; display: flex; box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.1); z-index: 100; border-top: 1rpx solid #eee; } .tab-item { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 22rpx; color: #999; } .tab-item.active { color: #0085ff; } .tab-item text { margin-top: 6rpx; } </style>
最新发布
06-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值