Parsing JSON file using JSONKit
Solution 1
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.
Solution 2
Using the parser in your question and assuming you have Simeon's string in an NSString variable. Here's how to parse it:
#import "JSONKit.h"
id parsedJSON = [myJSONString objectFromJSONString];
That will give you a hierarchy of arrays and dictionaries that you can walk to get your Preset
and Theme
objects. In the above case, you would get an array with two dictionaries each with a name
, comment
and presets
key. The first two will have NSString
values and the third (presets
) will have a dictionary as it's value with the note name as keys and the frequencies as values (as NSString
objects).
Related videos on Youtube
P i
Teen-coder (Linux/C++) -> math-grad -> tutor -> freelancer (Mobile specializing in Audio/DSP) -> Software Engineer -> DSP Consultant -> CTO (for cueaudio.com) -> Doing my own thing My most recent placement was as lead engineer for cueaudio.com. However my role quickly elevated to CTO and head of technical staffing. I was able to set the company on the right track by creatively sourcing key talent. The company recovered its $3M seed funding within the first 30 months of operation.
Updated on June 01, 2022Comments
-
P i almost 2 years
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?
-
Rahul Vyas almost 13 yearsUse jsonlint.com for validating your json.
-
-
P i almost 13 yearsThanks! This is really helpful! Is it unavoidable to use a gadzillion " marks everywhere?
-
P i almost 13 yearsThere is an error in the answer: it should be an NSArray not an NSMutableDictionary. It is an array of dictionary objects.
-
Simeon almost 13 years@P i: Thanks, I was actually unsure but never checked