zoukankan      html  css  js  c++  java
  • iOS开发之XML和JSON数据解析

           iOS开发中,我们往往需要从网络获取需要展示的数据,这些数据一般以XML或JSON格式表示,必须对其进行解析,得到我们可以处理的实体对象数据。所谓“解析”,就是从事先规定好的格式串中提取数据。解析的前提是数据的提供方与获取方提前约定好格式,数据提供方按照格式提供数据,数据获取方按照格式获取数据。一般情况下,我们没必要去自己设计数据交换格式。XML和JSON就是软件开发中通用的标准的结构化数据交换格式。

    一、什么是XML和JSON

      详细介绍请参考http://www.w3school.com.cn/xml/index.asphttp://www.w3schools.com/xml/default.asp。下面简单说明。

     1、XML

     XML指的是可扩展标记语言,即 EXtensible Markup Language 的缩写。如下就是一个完整的XML例子。

    <?xml version="1.0" encoding="UTF-8"?>
    <note>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
    </note>
    • XML被设计用来传输和存储数据,其焦点是数据的内容,XML使得数据结构化传输。
    • XML具有自我描述性,是机器和人都能读懂的标记语言。
    • 不像HTML有预定义标签,XML需要自行定义标签。

    2、JSON

    • JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)
    • JSON 是轻量级的文本数据交换格式
    • JSON 独立于语言 *
    • JSON 具有自我描述性,更易理解

    * JSON 使用 JavaScript 语法来描述数据对象,但是 JSON 仍然独立于语言和平台。JSON 解析器和 JSON 库支持许多不同的编程语言。

    JSON比XML更小、更快、更易解析,都是数据干货,没有XML那么多额外的描述标记。

    例如要表示一个包含 3 个员工记录(对象)的数组employee 对象,JSON和XML分别表示如下。

    JSON示例

    {"employees":[
        {"firstName":"John", "lastName":"Doe"},
        {"firstName":"Anna", "lastName":"Smith"},
        {"firstName":"Peter", "lastName":"Jones"}
    ]

    XML示例

    <employees>
        <employee>
            <firstName>John</firstName> <lastName>Doe</lastName>
        </employee>
        <employee>
            <firstName>Anna</firstName> <lastName>Smith</lastName>
        </employee>
        <employee>
            <firstName>Peter</firstName> <lastName>Jones</lastName>
        </employee>
    </employees>

    二、JSON解析

    1、JSON语法规则

    JSON 语法是 JavaScript 对象表示语法的子集。
    • 数据在键值对中
    • 数据由逗号分隔
    • 花括号保存对象
    • 方括号保存数组
    SON 数据的书写格式是:名称/值对。
    名称/值对组合中的名称写在前面(在双引号中),值对写在后面(同样在双引号中),中间用冒号隔开:
    "name":"xiaoming"
    JSON 值可以是:
    • 数字(整数或浮点数)
    • 字符串(在双引号中)
    • 逻辑值(true 或 false)
    • 数组(在方括号中)
    • 对象(在花括号中)
    • null

    2、JSON文档结构

    JSON文档有两种结构,对象和数组。

    对象:即最外层是对象,以“{“开始,以”}”结束。

    数组:即最外层是数组,以“["开始,以“]”结束。

    最外层为对象示例

    {
        "employees":[
        {"firstName":"John", "lastName":"Doe"}, 
        {"firstName":"Anna", "lastName":"Smith"}, 
        {"firstName":"Peter","lastName":"Jones"}]
    }

    最外层为数组示例

    [
        {"firstName":"John", "lastName":"Doe"}, 
        {"firstName":"Anna", "lastName":"Smith"}, 
        {"firstName":"Peter","lastName":"Jones"}
    ]

    3、解析API

    iOS5以后,官方提供了性能不错的NSJSONSerialization类。

    /* Generate JSON data from a Foundation object. If the object will not produce valid JSON then an exception will be thrown. 
    Setting the NSJSONWritingPrettyPrinted option will generate JSON with whitespace designed to make the output more readable.
    If that option is not set, the most compact possible JSON will be generated.
    If an error occurs, the error parameter will be set and the return value will be nil.
    The resulting data is a encoded in UTF-8.
    */ + (nullable NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error; /* Create a Foundation object from JSON data. Set the NSJSONReadingAllowFragments option if the parser should allow top-level objects that are not an NSArray or NSDictionary.
    Setting the NSJSONReadingMutableContainers option will make the parser generate mutable NSArrays and NSDictionaries.
    Setting the NSJSONReadingMutableLeaves option will make the parser generate mutable NSString objects.
    If an error occurs during the parse, then the error parameter will be set and the result will be nil. The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM.
    The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.
    */ + (nullable id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;

    当然还有第三方框架可供选择:JSONKit、SBJson、TouchJSON。

  • 相关阅读:
    基于OWin的Web服务器Katana发布版本3
    如何在.NET上处理二维码
    .NET开源OpenID和OAuth解决方案Thinktecture IdentityServer
    ASP.NET Identity V2
    Azure Redis Cache
    CentOS 7 安装RabbitMQ 3.3
    ASP.Net MVC 5 in Xamarin Studio 5.2
    Centos 7.0 安装Mono 3.4 和 Jexus 5.6
    CentOS下GPT分区(转)
    CentOS下使用LVM进行分区(转)
  • 原文地址:https://www.cnblogs.com/newhope/p/5281843.html
Copyright © 2011-2022 走看看