0

Currently, I have within my iPhone app a URL with which contains a JSON object that I must parse.

I am able to fetch the JSON object, convert the object to an NSString, now the issue is parsing the object/NSString object.

I am currently using SBJSON.

How do I go about iterating through the key elements of the JSON object using SBJSON's framework?

{
   "status":"SUCCESS",
   "fields":[
      {
         "type":"instant",
         "field":"GenerationPower",
         "label":"now",

The JSON object is MUCH larger than just these keys and key elements but once this issue is resolved, I'm sure the rest of the JSON object will be easy since i'll have a reference.

Thank you Stackoverflow!

EDIT: Here's some code to clarify my issue.

+ (NSString *) pullingInfo
{
    NSURL *solarWebURL = [NSURL URLWithString:myurl];

    if (solarWebURL)
    {
        NSLog(@"Calling: %@", solarWebURL);

        NSData *jsonData = [NSData dataWithContentsOfURL:solarWebURL];

        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

        return jsonString;
    }

    NSString* errorMessage = @"Error reading URL";
    return errorMessage;
}

+ (NSDictionary *) jsonDictionaryObject
{
    NSDictionary * jsonDictionary = [[NSDictionary alloc] init];

    NSString * monroeString = [MonroeParser pullingInfo];


    return jsonDictionary;
}

So as I said before, I have already loaded the JSON object into an NSString object "jsonString". Now I would like to start parsing the string.

I figure I may not even need to use JSON's framework for parsing, I can probably just parse the NSString using NSString conventions provided by Apple.

Any idea's? But maybe this isn't efficient....

2
  • 1
    for (NSString *key in parsedDictionary) { } Commented May 26, 2013 at 20:52
  • 2
    If you haven't already done so, be sure to go to json.org and spend 5-10 minutes learning the JSON syntax -- that's all it takes, and you will then understand how to "disassemble" complex JSON objects (after they're parsed into Cocoa objects by a suitable parser). Commented May 27, 2013 at 3:20

1 Answer 1

1

Sine you are using SBJSON, why are you even converting the NSData to an NSString? You can use -objectWithData method for SBJSONParser to directly read the NSData into an NSDictionary.

http://sbjson.org/api/3.2/Classes/SBJsonParser.html#//api/name/objectWithData:

Let pullingInfo return an id. And in you calling function check if the id is of type NSDictionary or NSArray and parse accordingly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.