V-Xtreme
V-Xtreme

Reputation: 7333

Parsing JSON to the Object

I have following JSON file to parse:

[
{
    "name": "kkkk",
    "empid": "55628",
    "address": "mumbai",
    "mobile": "9528558"
},
{
    "name": "xtreme",
    "empid": "20",
    "address": "stripes",
    "mobile": "9689597"
},
{
    "name": "xtreme",
    "empid": "20",
    "address": "stripes",
    "mobile": "9689999597"
},
{
    "name": "xtreme",
    "empid": "20",
    "address": "stripes",
    "mobile": "9689999597"
},
{
    "name": "xtreme",
    "empid": "20",
    "address": "stripes",
    "mobile": "9689597"
},
{
    "name": "xtreme",
    "empid": "20",
    "address": "stripes",
    "mobile": "9689999597"
},
{
    "name": "xtreme",
    "empid": "20",
    "address": "stripes",
    "mobile": "9689999597"
},
{
    "name": "xtreme",
    "empid": "20",
    "address": "stripes",
    "mobile": "9689999597"
},
{
    "name": "xtreme",
    "empid": "20",
    "address": "stripes",
    "mobile": "96897"
},
{
    "name": "vx",
    "empid": "96",
    "address": "addre",
    "mobile": "9689999596"
},
{
    "name": "vxx",
    "empid": "96",
    "address": "addre",
    "mobile": "968999"
},
{
    "name": "vx",
    "empid": "96",
    "address": "addre",
    "mobile": "9699596"
},
{
    "name": "vxertdrt",
    "empid": "96",
    "address": "addre",
    "mobile": "968996"
},
{
    "name": "vx",
    "empid": "96",
    "address": "addre",
    "mobile": "999596"
}

]

I have following code for parsing:

 SBJSON *parser=[[SBJSON alloc]init];
array=[parser objectWithString:firstParseData];
NSString *secondParseData=[array objectAtIndex:0];
NSLog(@"name=%@",secondParseData);

by this code i am getting the following output in string format:

 name={
address = mumbai;
empid = 55628;
mobile = 9525878558;
name = kkkk;

}

But i want the output in form of employee object. How can i made the the employee object from the json??

Upvotes: 0

Views: 119

Answers (2)

dimme
dimme

Reputation: 4424

You must have an Employee class with a constructor. Then you can do something like this. You don't need to create a SBJson parser object. You can use it directly on the NSString, that is a beauty of SBJson.

NSString * data = @"[
{
    "name": "kkkk",
    "empid": "55628",
    "address": "mumbai",
    "mobile": "9528558"
},
...
{
    "name": "vx",
    "empid": "96",
    "address": "addre",
    "mobile": "999596"
}
]";

// Get an array with all the employees inside
NSArray *myArray = [data JSONValue];

// Pick out the first employee
NSDictionary *firstEmployee = myArray[0];

// Create an object for him
Employee *firstEmployeeObject = [[Employee alloc] initWithAddress:[firstEmployee objectForKey:@"address"] andEmpID:[firstEmployee objectForKey:@"empid"] andMobile:[firstEmployee objectForKey:@"mobile"] andName:[firstEmployee objectForKey:@"name"]];

Don't forget to release your objects if you are not using ARC.

Upvotes: 1

Hector
Hector

Reputation: 3907

Try this :

NSMutableArray *final_array =[[NSMutableArray alloc]init];
for (NSDictionary *dict in array) {
    Employee *obj =[[Employee alloc]init];
    for (id key in [dict allKeys]) {
        [obj setValue:[dict objectForKey:key] forKey:key];
    }
    [final_array addObject:obj];
}

Upvotes: 1

Related Questions