使用JSONKit解析JSON文件 [英] Parsing JSON file using JSONKit

查看:130
本文介绍了使用JSONKit解析JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个音叉应用程序。叉子应该允许12个预先设定的音高。



此外,我希望允许用户选择一个主题。每个主题都会加载一组预设(不需要全部使用它们)。

我的配置文件看起来像这样*:

 主题:A3
评论:低于音乐会音高的八度(即A4 440Hz)
预设:{
A3 220Hz = 220.0
}

// http://en.wikipedia.org/wiki/Guitar_tuning
主题: 吉他标准调整
评论:EADGBE使用12-TET调音
预设:{
E2 = 82.41
A2 = 110.00
D3 = 146.83
G3 = 196.00
B3 = 246.94
E4 = 329.63
}

主题:低音吉他标准调整
评论:EADG使用12- TET调整
预设:{
E1 = 41.204
A2 = 55.000
D3 = 73.416
G3 = 97.999
}






...需要被提取到像这样的结构中:






  @class预设
{
NSString * label;
双倍频率;
}

@class主题
{
NSString * label;
NSMutableArray *预置;
}

NSMutableArray *主题;






如何使用JSON编写我的文件? (我想创建一个用户的最低限度的打字 - 我可以得到多么简洁?有人可以给我一个例子的第一个主题?)

如何使用 https://github.com/johnezang/JSONKit 将它解析到结构中?

解决方案

这是一个有效的 JSON示例根据您的想法:

$ p $ {
name:Guitar Standard Tuning,
comment:EADGBE using 12-TET tuning,
presets:{
E2:82.41,
A2:110.00,
D3:146.83,
G3:196.00,
B3:246.94,
E4:329.63

name:Bass Guitar Standard Tuning,
comment:EADG using 12-TET tuning,
presets :{
E1:41.204,
A1:55.000,
D2:73.416,
G2:97.999
}




$ b读取一个文件并使用JSONKit解析

  NSData * jsonData = [NSData dataWithContentsOfFile:path]; 
JSONDecoder * decoder = [[JSONDecoder alloc]
initWithParseOptions:JKParseOptionNone];
NSArray * json = [decoder objectWithData:jsonData];

之后,您必须迭代 json 使用for循环的变量。


I am constructing a tuning fork app. The fork should allow up to 12 preset pitches.

Moreover, I wish to allow the user to choose a theme. Each theme will load a set of presets (not necessary to use all of them).

My configuration file would look something like this*:


theme: "A3"
comment: "An octave below concert pitch (ie A4 440Hz)"
presets: {
    A3 220Hz=220.0
}

// http://en.wikipedia.org/wiki/Guitar_tuning
theme: "Guitar Standard Tuning"
comment:"EADGBE using 12-TET tuning"
presets: {
    E2=82.41
    A2=110.00
    D3=146.83
    G3=196.00
    B3=246.94
    E4=329.63
}

theme: "Bass Guitar Standard Tuning"
comment: "EADG using 12-TET tuning"
presets: {
    E1=41.204
    A2=55.000
    D3=73.416
    G3=97.999
}


...which need to be extracted into some structure like this:


@class Preset
{
    NSString* label;
    double freq;
}

@class Theme
{
    NSString* label;
    NSMutableArray* presets;
}

NSMutableArray* themes;


How do I write my file using JSON? ( I would like to create a minimum of typing on the part of the user -- how succinct can I get it? Could someone give me an example for the first theme? )

And how do I parse it into the structures using https://github.com/johnezang/JSONKit?

解决方案

Here's a valid JSON example based on your thoughts:

[
    {
        "name": "Guitar Standard Tuning",
        "comment": "EADGBE using 12-TET tuning",
        "presets": {
            "E2": "82.41",
            "A2": "110.00",
            "D3": "146.83",
            "G3": "196.00",
            "B3": "246.94",
            "E4": "329.63"
        }
    },
    {
        "name": "Bass Guitar Standard Tuning",
        "comment": "EADG using 12-TET tuning",
        "presets": {
            "E1": "41.204",
            "A1": "55.000",
            "D2": "73.416",
            "G2": "97.999"
        }
    }
]

Read a file and parse using JSONKit:

NSData* jsonData = [NSData dataWithContentsOfFile: path];
JSONDecoder* decoder = [[JSONDecoder alloc]
                             initWithParseOptions:JKParseOptionNone];
NSArray* json = [decoder objectWithData:jsonData];

After that, you'll have to iterate over the json variable using a for loop.

这篇关于使用JSONKit解析JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆