Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8994925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:31:55+00:00 2026-06-15T23:31:55+00:00

My application returns a NSMutableData *receivedData . I’ve opted for using NSJSONSerialization to parse

  • 0

My application returns a NSMutableData *receivedData.

I’ve opted for using NSJSONSerialization to parse this under the assumption that it would be easiest. I’m having extreme trouble trying to get my head around how to do it. I’m very new to Objective-C, from a Java background.

In Java I used gson to parse the JSON in to an array which I could use easily. I’m really struggling with this here.

My current code for parsing the JSON is:

NSError *e = nil;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: receivedData options: NSJSONReadingMutableContainers error: &e];

    if (!jsonArray) {
        NSLog(@"Error parsing JSON: %@", e);
    } else {
        for(NSDictionary *item in jsonArray) {
            NSLog(@"Item: %@", item);
        }
    }

As provided by somebody on the internet. This works and prints two items to NSLog. result and header. Here is how the JSON looks:

{
  "header":{
    "session":"sessionid",
    "serviceVersion":"1",
    "prefetchEnabled":true
  },
  "result":"50ce82401e826"
}

However if there is an error the JSON can also look like this:

{
  "header":{
    "session":"sessionid",
    "serviceVersion":"1",
    "prefetchEnabled":true
  },
  "fault":{
    "code":0,
    "message":"someErrorCode"
  }
}

How I want the code to work:

  1. Check if there is a “fault” object
  2. If there is, print fault.code and fault.message to NSLog
  3. If there isn’t, I know that my JSON contains result instead of fault
  4. Print the value of result to NSLog

But I can’t for the life of me figure out how to approach it. Can someone please give me some pointers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:31:57+00:00Added an answer on June 15, 2026 at 11:31 pm

    your object appears to be a dictionary.

    Try this out.

    NSError *e = nil;
    id jsonObj = [NSJSONSerialization JSONObjectWithData: receivedData options: NSJSONReadingMutableContainers error: &e];
    
    NSArray *jsonArray = nil;
    NSDictionary *jsonDict = nil;
    
    if ([jsonObj isKindOfClass:[NSArray class]]){
        jsonArray = (NSArray*)jsonObj;
    }
    else if ([jsonObj isKindOfClass:[NSDictionary class]]){
        jsonDict = (NSDictionary*)jsonObj;
    }
    
    if (jsonArray != nil) {
        // you have an array;
        for(NSDictionary *item in jsonArray) {
            NSLog(@"Item: %@", item);
        }
    }
    else if (jsonDict != nil){
        for (NSString *key in jsonDict.allKeys){
            NSLog(@"Key: %@ forItem: %@",key,[jsonDict valueForKey:key]);
        }
    }
    else {
        NSLog(@"Error: %@",e);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am consuming a webservice using my java web application.This returns a number of
I have an application in which I have a webservice call that returns data
I just built an application in ASP.net that returns a PNG image. The resulting
I have my main application delegate which contains a method that returns an object.
In my wp7 application I do a web request that returns a status code
When I deployed my application on the server I faced this issue: Request.Url.ToString(); returns
In my web application, I have a dynamic query that returns huge data to
I have a Windows console application that returns some text. I want to read
Calling IEnumSTATURL::Next within my application returns E_UNEXPECTED(0x8000FFFF) instead of S_OK . Sadly, that particular
I have an application which returns JSONObject like this: Result: { status: 200, data:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.