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?

inot
  • 141
  • 1
  • 1
  • 4

6 Answers6

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"]);
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
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"]);
}
inot
  • 141
  • 1
  • 1
  • 4
  • 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 }) – Vineesh TP Aug 06 '12 at 04:53
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"];
Jstngoulet
  • 1,055
  • 7
  • 12
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;
}
inot
  • 103
  • 1
  • 10
1

try this

while (item = (NSDictionary*)[enumerator nextObject]) {
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray AddObject:((NSDictionary*)[enumerator nextObject])];
}
Luke
  • 11,426
  • 43
  • 60
  • 69
Radim Halfar
  • 147
  • 1
  • 12
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
}
prajakta
  • 673
  • 6
  • 26