简体   繁体   中英

Parsing JSON object for iOS application

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....

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM