本文主要是介绍JSON扫盲帖,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
[心得] JSON扫盲帖+JSON类教程
昨天发了json的AS3解析类,从大家的跟帖上来看,好多人还是不知道这个东西的,特建一个扫盲贴。 其实在AS中使用json其实并不是一个必须或是很好的选择,因为AS对xml的解析已经很不错了,但是为什么可以考虑使用json呢,有以下几点:
JSON 是什么? 简单 JSON 示例 按照最简单的形式,可以用下面这样的 JSON 表示名称/值对:
值的数组 当需要表示一组值时,JSON 不但能够提高可读性,而且可以减少复杂性。例如,假设您希望表示一个人名列表。在 XML 中,需要许多开始标记和结束标记;如果使用典型的名称/值对(就像在本系列前面文章中看到的那种名称/值对),那么必须建立一种专有的数据格式,或者将键名称修改为 person1-firstName 这样的形式。 如果使用 JSON,就只需将多个带花括号的记录分组在一起:
在处理 JSON 格式的数据时,没有需要遵守的预定义的约束。所以,在同样的数据结构中,可以改变表示数据的方式,甚至可以以不同方式表示同一事物。 ps:以上例子都是来自http://www.ibm.com/developerworks/cn/web/wa-ajaxintro10/,掌握 Ajax,自己懒,看人家有就顺便用了 ![]() 当当当,我又回来了,上周忙这上班没顾上写用法,下面就介绍一下adobe的json类的用法。 上次发表的json类有问题(http://bbs.actionscript3.cn/thread-1625-1-1.html),因为我也是从别人处下载的,谁知道是一个半成品。望大家别生气啊,建议管理员给删除了! 这次是adobe的官方的类,我修改了一下包,这次就可以用了。 下面是教程,比较简单: 1、服务器端来的json 怎么样获得服务器端的json我就不说了吧(就是通讯),那么得到的应该是一个字符串,存入变量serverJSON,使用方式如下:
json就是一个对象了,简单吧。 举一个例子: 上面的JSON的一段代码:
代码:
不是吧这么简单。其实转变后就成为一个对象了,可以通过点语法来访问这些值了。XML靠边去。 2、本地对象做成JSON 你要是能自己拼出JSON字符串也可以,不过我们是在面向对象的世界啊,那么我们都是对象啊,到时候对象直接就可以来用了。 举一个例子:
这样就可以给服务器了。 总结:就两个方法,JSON.decode(String),JSON.encode(Object),有这么简单的方式实现传输量小,而且简单的数据格式,我们为什么还不用呢? 其实XML自然也有他自己的强势,当一个结构复杂的数据结构出现的时候,这个时候JSON就很难搞定了,XML就是首选了。 |
要用到JSON,看了一篇(http://bbs.actionscript3.cn/thread-1657-1-1.html )的扫盲贴,在Flex中使用Json十分方便。json是介于纯文本方式与xml方式之间的一种格式,json能做到的事情,xml完成可以做到。为什么要用json呢,我看大部分还是像我一样,不得不用。json是ajax数据传输的首选,现有的项目使用的已经是json,如果增加flex界面时也使用json,那改动就会非常少。扫盲贴中说出的三个理由,我觉得这一个最充分。
要在flex中使用json,首先要下一个json包。是官方的,却在官方怎么也载不下来。终于在CSDN上用了3分才down下来。放在附件中,需要的可以拿去用。
贴一个例子:

































输出为:
zhanzhihu
{"firstName":"bill","lastName":"Gate","com":"Microsoft"}
jsonStr是一个json格式的字符串,用JSON.decode(String)便可以将它解析为as对象,同样用JSON.encode(Object)可以将as对象转为json字符串,flex前台对json的操作真是方便。
corelib下载地址:http://code.google.com/p/as3corelib/
[转]Flex用JSON处理返回的数据
关键字: flex json- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute"
- creationComplete="service.send()">
- <mx:Script>
- <![CDATA[
- import mx.collections.ArrayCollection;
- import mx.rpc.events.ResultEvent;
- import com.adobe.serialization.json.JSON;
- private function onJSONLoad(event:ResultEvent):void
- {
- //get the raw JSON data and cast to String
- var rawData:String = String(event.result);
- //decode the data to ActionScript using the JSON API
- //in this case, the JSON data is a serialize Array of Objects.
- var arr:Array = (JSON.decode(rawData) as Array);
- //create a new ArrayCollection passing the de-serialized Array
- //ArrayCollections work better as DataProviders, as they can
- //be watched for changes.
- var dp:ArrayCollection = new ArrayCollection(arr);
- //pass the ArrayCollection to the DataGrid as its dataProvider.
- grid.dataProvider = dp;
- }
- ]]>
- </mx:Script>
- <mx:HTTPService id="service" resultFormat="text"
- url="http://weblogs.macromedia.com/mesh/mashedpotato.json"
- result="onJSONLoad(event)" />
- <mx:DataGrid id="grid" right="10" left="10" top="10" bottom="10">
- <mx:columns>
- <mx:DataGridColumn headerText="Service" dataField="src"/>
- <mx:DataGridColumn headerText="Title" dataField="title"/>
- </mx:columns>
- </mx:DataGrid>
- </mx:Application>
使用Flex,Java,Json更新Mysql数据【1】 | ||||||||||||||||||
![]() | ||||||||||||||||||
|
这篇关于JSON扫盲帖的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!