1.json的源码:可以下载as3corelib核心包 里面有详细的介绍
2.通过json的测试类,我们可以很容易的理解和使用json转义成任何flex对象
请看json的测试类 JSONTest.as,这里我就不解释了,内容比较的简单,
package com.adobe.serialization.json
{
import flexunit.framework.TestCase;
public class JSONTest extends TestCase {
public function JSONTest( methodName:String = null) {
super( methodName );
}
/**
* Test for the JSON string true decoded to boolean true.
*/
public function testDecodeTrue():void {
var o:* = JSON.decode( " \n true \n " ) as Boolean;
assertTrue( "Expected decoded true", o == true );
}
public function testDecodeFalse():void {
var o:* = JSON.decode( " false " ) as Boolean;
assertTrue( "Expected decoded false", o == false );
}
public function testDecodeNull():void
{
var o:* = JSON.decode( "null " );
assertTrue( "Expected decoded null", o == null );
}
public function testDecodeString():void
{
var o:* = JSON.decode( ' "this is \t a string \n with \' escapes" ' ) as String;
assertTrue( "String not decoded successfully", o == "this is \t a string \n with \' escapes" );
o = JSON.decode( ' "http:\/\/digg.com\/security\/Simple_Digg_Hack" ' );
assertTrue( "String not decoded correctly", o == "http://digg.com/security/Simple_Digg_Hack" );
}
public function testDecodeZero():void
{
var n:Number = JSON.decode( "0" ) as Number;
assertEquals( n, 0 );
}
public function testDecodeZeroWithDigitsAfterIt():void
{
var parseError:JSONParseError = null;
try
{
var n:Number = JSON.decode( "02" ) as Number;
}
catch ( e:JSONParseError )
{
parseError = e;
}
finally
{
//trace( parseError.message );
assertNotNull( parseError );
}
}
public function testDecodePositiveInt():void
{
var n:int = JSON.decode( "123871" ) as int;
assertEquals( n, 123871 );
}
public function testDecodeNegativeInt():void
{
var n:int = JSON.decode( "-97123" ) as int;
assertEquals( n, -97123 );
}
public function testDecodePositiveFloat():void
{
var n:Number = JSON.decode( "12.987324" ) as Number;
assertEquals( n, 12.987324 );
}
public function testDecodeNegativeFloat():void
{
var n:Number = JSON.decode( "-1298.7324" ) as Number;
assertEquals( n, -1298.7324 );
}
public function testDecodeFloatLeadingZeroError():void
{
var parseError:JSONParseError = null;
try
{
var n:Number = JSON.decode( "-.2" ) as Number;
}
catch ( e:JSONParseError )
{
parseError = e;
}
finally
{
trace( parseError.message );
assertNotNull( parseError );
}
}
public function testDecodeFloatDecimalMissingError():void
{
var parseError:JSONParseError = null;
try
{
var n:Number = JSON.decode( "1." );
}
catch ( e:JSONParseError )
{
parseError = e;
}
finally
{
// Make sure we caught a parse error since
// there has to be numbers after the decimal
assertNotNull( parseError );
}
}
public function testDecodeScientificRegularExponent():void
{
var n:Number = JSON.decode( "6.02e2" ) as Number;
assertEquals( n, 602 );
n = JSON.decode( "-2e10" );
assertEquals( n, -20000000000 );
assertEquals( n, -2 * Math.pow( 10, 10 ) );
}
public function testDecodeScientificPositiveExponent():void
{
var n:Number = JSON.decode( "2E+9" ) as Number;
assertEquals( n, 2 * Math.pow( 10, 9 ) );
n = JSON.decode( "-2.2E+23" ) as Number;
assertEquals( n, -2.2 * Math.pow( 10, 23 ) );
}
public function testDecodeScientificNegativeExponent():void
{
var n:Number = JSON.decode( "6.02e-23" ) as Number;
assertEquals( n, 6.02 * Math.pow( 10, -23 ) );
n = JSON.decode( "-4e-9" ) as Number;
assertEquals( n, -4 * Math.pow( 10, -9 ) );
n = JSON.decode( "0E-2" ) as Number;
assertEquals( n, 0 );
}
public function testDecodeScientificExonentError():void
{
var parseError:JSONParseError = null;
try
{
var n:Number = JSON.decode( "1e" );
}
catch ( e:JSONParseError )
{
parseError = e;
}
finally
{
// Make sure we caught a parse error since
// there has to be numbers after the e
assertNotNull( parseError );
}
}
// Commented out - this is something that should only be available when "strict" is
// false.
// public function testDecodeHexNumber():void
// {
// var n:Number = JSON.decode( "0xFF0033" );
// assertEquals( 0xFF0033, n );
//
// var parseError:JSONParseError = null;
// try
// {
// n = JSON.decode( "0xZ" );
// }
// catch ( e:JSONParseError )
// {
// parseError = e;
// }
// finally
// {
// // Make sure we catch a parse error since 0xZ is an invalid number
// assertNotNull( parseError );
// }
// }
public function testDecodeObject():void {
var o:* = JSON.decode( " { \"test\": true, \"test2\": -12356732.12 } " ) as Object;
assertTrue( "Expected decoded object.test = true", o.test == true );
assertTrue( "Expected decoded object.test2 = -12356732.12", o.test2 == -12356732.12 );
}
public function testDecodeArray():void {
var o:* = JSON.decode( " [ null, true, false, 100, -100, \"test\", { \"foo\": \"bar\" } ] " ) as Array;
assertTrue( "Expected decoded array[0] == null", o[0] == null );
assertTrue( "Expected decoded array[1] == true", o[1] == true );
assertTrue( "Expected decoded array[2] == false", o[2] == false );
assertTrue( "Expected decoded array[3] == 100", o[3] == 100 );
assertTrue( "Expected decoded array[4] == -100", o[4] == -100 );
assertTrue( "Expected decoded array[5] == \"test\"", o[5] == "test" );
assertTrue( "Expected decoded array[6].foo == \"bar\"", o[6].foo == "bar" );
}
public function testDecodeArrayWithNewlines():void {
var o:Array = JSON.decode( "\n [ \nnull, \n \r \t \r true \n ] \r \t \r \n" ) as Array;
assertTrue( "Expected decoded with newlines array[0] == null", o[0] == null );
assertTrue( "Expected decoded with newlines array[1] == true", o[1] == true );
}
public function testDecodeSuccessiveComments():void
{
var jsonString:String = " // test comment"
+ "\n // test comment line 2"
+ "\nfalse";
var o:* = JSON.decode( jsonString );
assertEquals( false, o );
}
public function testEncodeTrue():void {
var o:String = JSON.encode( true );
assertTrue( "Expected encoded true", o == "true" );
}
public function testEncodeFalse():void {
var o:String = JSON.encode( false );
assertTrue( "Expected encoded false", o == "false" );
}
public function testEncodeNull():void {
var o:String = JSON.encode( null );
assertTrue( "Expected encoded null", o == "null" );
}
public function testEncodeString():void {
var o:String = JSON.encode( "this is a \n \"string\"" );
assertTrue( "Expected encoded string", o == "\"this is a \\n \\\"string\\\"\"" );
o = JSON.encode( "myString" );
assertEquals( o, "\"myString\"" );
}
public function testEncodeArrayEmpty():void {
var o:String = JSON.encode( [] );
assertTrue( "Expected encoded []", o == "[]" );
}
public function testEncodeArray():void {
var o:String = JSON.encode( [ true, false, -10, null, Number.NEGATIVE_INFINITY ] );
assertTrue( "Expected encoded array", o == "[true,false,-10,null,null]" );
}
public function testEncodeObjectEmpty():void {
var o:String = JSON.encode( {} );
assertTrue( "Expected encoded {}", o == "{}" );
}
public function testEncodeObject():void {
// Note: because order cannot be guaranteed when decoding
// into a string, we can't reliably test an object with
// multiple properties, so instead we test multiple
// smaller objects
//var obj:Object = new Object();
//obj.test1 = true;
//obj["test 2"] = false;
//obj[" test _3" ] = { foo: "bar" };
//var o:String = JSON.encode( obj );
//assertTrue( "Expected encoded object", o == "{\"test1\":true,\"test 2\":false,\" test _3\":{\"foo\":\"bar\"}}" );
var obj:Object = { foo: { foo2: { foo3: { foo4: "bar" } } } };
var s:String = JSON.encode( obj );
assertTrue( "Deeply nested", "{\"foo\":{\"foo2\":{\"foo3\":{\"foo4\":\"bar\"}}}}" );
obj = new Object();
obj[" prop with spaces "] = true;
assertTrue( "Prop with spaces", "{\" prop with spaces \":true}" );
}
public function testEncodeClassInstance():void
{
var s:String = JSON.encode( new SimpleClass() );
assertTrue( "Has length", s.length > 0 );
// Decode the string so we can verify that it has the properties
var o:Object = JSON.decode( s );
assertNotNull( o );
assertNotNull( o.publicVar1 );
assertNotNull( o.publicVar2 );
assertNotNull( o.accessor1 );
assertNotNull( o.accessor2 );
assertEquals( 17, o.publicVar1 );
assertEquals( 20, o.publicVar2 );
assertEquals( 25, o.accessor1 );
assertEquals( 30, o.accessor2 );
// Make sure o only has 4 properties
var count:int = 0;
for ( var key:String in o )
{
count++;
}
assertEquals( 4, count );
}
public function testDecodeEmptyStringError():void
{
var e:Error = null;
try
{
JSON.decode( "" );
}
catch ( pe:JSONParseError )
{
e = pe;
}
assertNotNull( e );
assertTrue( "Caught parse error", e is JSONParseError );
}
public function testWhiteSpace():void
{
/*
ws = *(
%x20 / ; Space
%x09 / ; Horizontal tab
%x0A / ; Line feed or New line
%x0D ; Carriage return
)
*/
//tabs
var a_tab:String = "[\t1\t]";
var a_tab_result:Array = JSON.decode(a_tab) as Array;
assertEquals(a_tab_result[0], 1);
//space
var a_space:String = "[ 1 ]";
var a_space_result:Array = JSON.decode(a_space) as Array;
assertEquals(a_space_result[0], 1);
//line return
var a_lr:String = "[\n1\n]";
var a_lr_result:Array = JSON.decode(a_lr) as Array;
assertEquals(a_lr_result[0], 1);
//carriage return
var a_cr:String = "[\r1\r]";
var a_cr_result:Array = JSON.decode(a_cr) as Array;
assertEquals(a_cr_result[0], 1);
//combined return
var a_clr:String = "[\n\r1\n\r]";
var a_clr_result:Array = JSON.decode(a_clr) as Array;
assertEquals(a_clr_result[0], 1);
}
}
}
比较有用的,而且需要注意的地方
var o:* = JSON.decode( " [ null, true, false, 100, -100, \"test\", { \"foo\": \"bar\" } ] " ) as Array;
在转换成数组的时候,{ \"foo\": \"bar\" } 对象的属性值需转义为字符串!不能写成如下:{foo: \"bar\" }
分享到:
相关推荐
`JSON.parse()`方法是解析JSON字符串的关键,它接收一个JSON格式的字符串作为参数,返回一个ActionScript对象(可能是Array、Object或其他类型),可以直接在Flex应用中使用。例如: ```actionscript var json...
在Flex开发中,JSON(JavaScript Object Notation)是一种常见的数据交换格式,因其轻量级、易读易写的特点,被广泛用于Web服务与客户端之间的数据通信。本压缩包"flex json解析包(corelib.swc)"是针对Flex应用...
通过使用Flex_json包,开发者可以轻松地将ActionScript对象转换为JSON字符串,发送给服务器;同样,也可以将接收到的JSON字符串解析成ActionScript对象,方便在客户端进行处理。 这个下载的Flex_json包包含了处理...
在Flex中,为了实现JSON的序列化和反序列化,开发者通常会使用第三方库,如FlexJSON。FlexJSON库提供了一组API,使得Flex能够将ActionScript对象转换为JSON字符串,反之也可以将JSON字符串解析成ActionScript对象。...
在给定的资源中,“flex解析json的swc包(entity-flex-v1-02)及例子”显然提供了用于在Flex应用中处理JSON数据的库。SWC是Adobe Flex的库文件格式,它包含了编译后的ActionScript代码和相关的元数据,可以被其他Flex...
### Flex使用json和xml数据交互:深度解析与实践 #### 引言 在现代Web开发领域,数据交换格式的选择对于实现高效、灵活的前后端通信至关重要。JSON(JavaScript Object Notation)与XML(Extensible Markup ...
将服务器返回的JSON数据转换为AS3对象后,可以轻松地在Flex应用中使用这些数据,实现动态内容更新。 - **Flex与Java的集成**:文章中提到的Flex与Java的配合,主要体现在Flex作为前端UI框架,Java作为后端服务器...
Flex JSON库的使用大大简化了ActionScript与JSON数据的交互,使得开发基于Flex的Web应用程序时能更有效地处理服务器通信。同时,`HTTPService`组件提供了便利的接口来执行HTTP请求,是Flex开发中不可或缺的一部分。...
JSON在Flex3中的使用涉及到两个主要类:`JSON`和`ObjectProxy`。`JSON`类提供了一系列静态方法,如`parse()`和`stringify()`,用于在ActionScript对象和JSON字符串之间进行转换。`parse()`方法接收一个JSON字符串并...
本篇将详细介绍在Flex中如何使用JSON,包括JSON的基本概念、解析与序列化的过程,并结合实际例子进行深入探讨。 一、JSON基础 JSON是一种基于文本的格式,它模仿了JavaScript对象的语法。一个JSON数据通常由键值对...
总的来说,Adobe提供的Flex解析Json的ActionScript包是Flex开发者处理JSON数据不可或缺的一部分,它简化了数据交换的过程,使得Flex应用能够轻松地与使用JSON的后端服务进行交互。无论是在创建动态图表、数据驱动的...
标题中的"FLEX JSON 包"指的是在Adobe Flex开发中用于处理JSON(JavaScript Object Notation)数据的库或组件。Flex是一种开源的、基于...通过学习和熟练掌握JSON的使用,可以显著提升Flex应用的开发效率和用户体验。
本话题聚焦于使用Flex处理JSON数据,将其转化为无限层级的树形结构,并添加单选框功能,以便用户选择特定节点,同时能够获取到被选中的子项。这个功能在诸如文件管理系统、组织架构展示、菜单导航等场景中非常常见。...
flex前端json序列号包,将object类型序列号成json格式
com.adobe.serialization.json.JSON; flex使用json报错,没有定义的类型,导入包即可. 资源是rar文件, 里面有源码+示例 和一个as3corelib.swc文件
可以很好地将JSon字符串转换为Flex对象,很方法JParser.encode()正好相反,其它的使用请参考JSwoof官方网站的文档:http://www.waynemike.pwp.blueyonder.co.uk/jswoof/sub-pages/getting-started.html或者资源包中...
标题 "flex 需要的json包" 暗示我们正在讨论的是关于Adobe Flex中使用的JSON库。Flex是一个开放源代码的框架,用于构建富互联网应用程序(RIA),它使用ActionScript编程语言和MXML标记语言。在Flex中,处理JSON...
Flex JSON开发是一个涉及前端与后端数据交互的重要技术,它主要使用Adobe Flex框架与JSON(JavaScript Object Notation)数据格式进行交互。Flex是基于ActionScript的开放源代码框架,用于构建富互联网应用程序(RIA...
标题中的“Flex使用JSON格式与Java通信”是指在开发富互联网应用程序(Rich Internet Application, RIA)时,采用Adobe Flex作为前端技术,通过JSON(JavaScript Object Notation)数据交换格式与后端Java服务器进行...