JSON数据

JSON数据


JSON(JavaScript Object Notation),是一种基于文本,独立于语言的轻量级数据交换格式;类似 XML,但比后者更小、更快,更易解析。

  • 语法

  • 数据保存在键/值对

  • 数据由逗号分隔

  • 花括号/{ }保存对象

  • 方括号/[ ]保存数组

  • 键值对

  • 必须是字符串

  • 可以以下任意一种:

  1. 数字(整数或浮点数)--->OC中对应NSNumber
  2. 字符串(“ ”)
  3. 逻辑值(true 或 false)
  4. 数组([ ])
  5. 对象({ })
  6. null(空)
  • 对象
  • 一个JSON对象可以包含多个键值对,类似于OC字典。
{ "firstName":"John" , "lastName":"Doe" }
  • 数组
  • 一个JSON数组可以包含多个对象,类似于OC数组。
    {
        "employees": [
        { "firstName":"John" , "lastName":"Doe" },
        { "firstName":"Anna" , "lastName":"Smith" },
        { "firstName":"Peter" , "lastName":"Jones" }
                      ]
    }
  • JSON文件
  • 文件类型:.json
  • MIME类型:application/json

  • JSON解析
  • 使用NSJSONSerialization将JSON数据转化为OC对象结构,或反之。
  • 将一个对象转化为JSON必须符合以下条件:
  • 对象结构的最顶层必须是一个NSArray /NSDictionary
  • 对象结构只能由NSStringNSNumberNSArrayNSDictionaryNSNull组成。
  • 字典的键必须是NSString
  • 其中包含的数字不能是无穷数或非数值

  • 与OC对象结构
  • JSON数据结构
{
  "DTSDKName" : "iphonesimulator9.1",
  "CFBundleName" : "JSON-Player",
  "DTXcode" : "0710",
  "UILaunchStoryboardName" : "LaunchScreen",
  "DTSDKBuild" : "13B137",
  "CFBundleDevelopmentRegion" : "en",
  "CFBundleVersion" : "1",
  "BuildMachineOSBuild" : "15B42",
  "DTPlatformName" : "iphonesimulator",
  "CFBundleShortVersionString" : "1.0",
  "UIMainStoryboardFile" : "Main",
  "CFBundlePackageType" : "APPL",
  "CFBundleSupportedPlatforms" : [
    "iPhoneSimulator"
  ],
  "CFBundleInfoDictionaryVersion" : "6.0",
  "UIRequiredDeviceCapabilities" : [
    "armv7"
  ],
  "CFBundleExecutable" : "JSON-Player",
  "DTCompiler" : "com.apple.compilers.llvm.clang.1_0",
  "MinimumOSVersion" : "9.1",
  "CFBundleIdentifier" : "com.lofter.fever105.JSON-Player",
  "UIDeviceFamily" : [
    1
  ],
  "DTXcodeBuild" : "7B91b",
  "DTPlatformVersion" : "9.1",
  "LSRequiresIPhoneOS" : true,
  "UISupportedInterfaceOrientations" : [
    "UIInterfaceOrientationPortrait",
    "UIInterfaceOrientationLandscapeLeft",
    "UIInterfaceOrientationLandscapeRight"
  ],
  "CFBundleSignature" : "????",
  "DTPlatformBuild" : ""
}

  • OC对象结构
{
    BuildMachineOSBuild = 15B42;
    CFBundleDevelopmentRegion = en;
    CFBundleExecutable = "JSON-Player";
    CFBundleIdentifier = "com.lofter.fever105.JSON-Player";
    CFBundleInfoDictionaryVersion = "6.0";
    CFBundleName = "JSON-Player";
    CFBundlePackageType = APPL;
    CFBundleShortVersionString = "1.0";
    CFBundleSignature = "????";
    CFBundleSupportedPlatforms =     (
        iPhoneSimulator
    );
    CFBundleVersion = 1;
    DTCompiler = "com.apple.compilers.llvm.clang.1_0";
    DTPlatformBuild = "";
    DTPlatformName = iphonesimulator;
    DTPlatformVersion = "9.1";
    DTSDKBuild = 13B137;
    DTSDKName = "iphonesimulator9.1";
    DTXcode = 0710;
    DTXcodeBuild = 7B91b;
    LSRequiresIPhoneOS = 1;
    MinimumOSVersion = "9.1";
    UIDeviceFamily =     (
        1
    );
    UILaunchStoryboardName = LaunchScreen;
    UIMainStoryboardFile = Main;
    UIRequiredDeviceCapabilities =     (
        armv7
    );
    UISupportedInterfaceOrientations =     (
        UIInterfaceOrientationPortrait,
        UIInterfaceOrientationLandscapeLeft,
        UIInterfaceOrientationLandscapeRight
    );
}


  • 与XML
  • 相同之处:
  1. 都是纯文本。
  2. 自我描述,即具有可读性。
  3. 通过层级结构存储数据。
  4. 可以被JavaScript解析。
  5. 可以通过AJAX传输。
  • 不同之处:
  1. 没有结束标签。
  2. 更短。
  3. 读写速度更快。
  4. 具有数组结构。
  5. 没有保留字。
  6. 能够被JavaScript eval()方法解析。

你可能感兴趣的:(JSON数据)