Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON file using JSONKit

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?

like image 369
P i Avatar asked Jul 12 '11 11:07

P i


1 Answers

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.

like image 177
Simeon Avatar answered Sep 21 '22 21:09

Simeon