8

I have JSON object like this :

{ "data":
  {"array":
    ["2",
       {"array":
          [
            {"clientId":"1","clientName":"Andy","job":"developer"},
            {"clientId":"2","clientName":"Peter","job":"carpenter"}
          ]
        }
     ]
   },
 "message":"MSG0001:Success",
 "status":"OK"
}

I want to get the array[0] value (2) and array[1] value (clientId, clientName, job) using JSON-Framework. Do you have any idea how to do that?

1
  • 9
    Sorry if I might sound rude, but google for iphone parse json, first hit. It is a good tutorial imoh. Commented Jul 2, 2010 at 11:40

6 Answers 6

21

Assuming you've followed the instructions to install JSON-Framework into your project, here's how you use it (taken from the docs here) :

// Parse the string into JSON
NSDictionary *json = [myString JSONValue];

// Get the objects you want, e.g. output the second item's client id
NSArray *items = [json valueForKeyPath:@"data.array"];
NSLog(@" client Id : %@", [[items objectAtIndex:1] objectForKey:@"clientId"]);
Sign up to request clarification or add additional context in comments.

1 Comment

Typo. Shouldn't it be [[items objectAtIndex:1] objectForKey:@"clientId"]);
6

thank you for your answer, my problem solved, I modify a little bit from your code, here are:

// Parse the string into JSON
NSDictionary *json = [myString JSONValue];

// Get all object
NSArray *items = [json valueForKeyPath:@"data.array"];
NSArray *array1 = [[items objectAtIndex:1] objectForKey:@"array"];
NSEnumerator *enumerator = [array1 objectEnumerator];
NSDictionary* item;
while (item = (NSDictionary*)[enumerator nextObject]) {
   NSLog(@"clientId = %@",  [item objectForKey:@"clientId"]);
   NSLog(@"clientName = %@",[item objectForKey:@"clientName"]);
   NSLog(@"job = %@",       [item objectForKey:@"job"]);
}

1 Comment

I have a time API in json format how can i use this, please help me. jsont({ "id": "ntp-a1.nict.go.jp", "it": 1232963971.248, "st": 1344228610.961, "leap": 34, "next": 1341100800, "step": 1 })
1

We need 1 class, let say MyData.h and MyData.m

//MyData.h
@interface MyData : NSObject {
    NSString *clientId;
    NSString *clientName;
    NSString *job;
}

@property (nonatomic, retain) NSString *clientId;
@property (nonatomic, retain) NSString *clientName;
@property (nonatomic, retain) NSString *job;

@end

//MyData.m
@implementation MyData

@synthesize clientId, clientName, job;

- (void)dealloc{    
    [clientId release];
    [clientName release];
    [job release];
    [super dealloc];
}

@end
//-------------------------------------

To store our data :

NSMutableArray *dataArray = [[NSMutableArray alloc]init];
while (item = (NSDictionary*)[enumerator nextObject]) {
    MyData *aMyData = [[MyData alloc] init];
    aMyData.clientId   = [item objectForKey:@"clientId"];
    aMyData.clientName = [item objectForKey:@"clientName"];
    aMyData.job        = [item objectForKey:@"job"];
    [dataArray addObject:aMyData];
    [aMyData release];
    aMyData = nil;
}

Comments

1

try this

while (item = (NSDictionary*)[enumerator nextObject]) {
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray AddObject:((NSDictionary*)[enumerator nextObject])];
}

Comments

1

You can create a hierarchy of data points. For example, if you want to get the inner array of a JSON object, you can access it using:

NSArrray *objectArray = jsonObject[@"array"][@"2"][@"array"];

Or, you can do something similar. For example, in the Yelp API, you are provided a a JSON of a business. Placing these businesses in an array, you can access different elements of the object by doing:

NSString *businessLocation = [businessArray objectAtIndex: indexOfBusinessInArray][@"location"][@"display_address"];

Comments

0

how to store this data in NSMUtableArray ??

while (item = (NSDictionary*)[enumerator nextObject]) {
   NSLog(@"clientId = %@",  [item objectForKey:@"clientId"]);//this 
   NSLog(@"clientName = %@",[item objectForKey:@"clientName"]);//this
   NSLog(@"job = %@",       [item objectForKey:@"job"]);//this
}

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.