-1

I am new to iPhone development. I got json as response.

How to separate the Drug value from the json string below:

{"arrCDrugEntity":
    [
       {
       "DrugID":1,
       "Drug":"Benadril",
       "Quantity":"",
       "Comment":"",
       "FunctionStatus":false,
       "ResultString":"",
       "ErrorString":""
       },
       {
       "DrugID":2,
       "Drug":"Dcold",
       "Quantity":"",
       "Comment":"",
       "FunctionStatus":false,
       "ResultString":"",
       "ErrorString":""
       },  
       { 
       "DrugID":3,
       "Drug":"Dolo",
       "Quantity":"",
       "Comment":"",
       "FunctionStatus":false,
       "ResultString":"",
       "ErrorString":""
       },  
       {
       "DrugID":4,
       "Drug":"Paracitamol",
       "Quantity":"",
       "Comment":"",
       "FunctionStatus":false,
       "ResultString":"",
       "ErrorString":""
        },
        { 
        "DrugID":5,
        "Drug":"Panadol",
        "Quantity":"",
        "Comment":"",
        "FunctionStatus":false,
        "ResultString":"",
        "ErrorString":""
        },
        {
        "DrugID":6,
        "Drug":"Pudin Hara",
        "Quantity":"",
        "Comment":"",
        "FunctionStatus":false,
        "ResultString":"",
        "ErrorString":""
         }
    ],
    "FunctionStatus":true,
    "UserID":-1,
    "DeliveryAddress":"",
    "ResultString":"",
    "ErrorString":""
}
Nipun
  • 990
  • 1
  • 16
  • 25
THOMAS
  • 129
  • 1
  • 8

5 Answers5

1
NSString *str=@"http://your_web_server/your_file....";
NSURL *url=[NSURL URLWithString:str];
        NSData *data=[NSData dataWithContentsOfURL:url];
        NSError *error=nil;
        id *response=[NSJSONSerialization JSONObjectWithData:data options:
                                NSJSONReadingMutableContainers error:&error]; 

NSLog("Your JSON Object: %@ Or Error is: %@, response, error);

Following code for retrive json data

Dictionary *json = [myString JSONValue];

// Get the objects you want, e.g. output the second item's client id
NSArray *items = [json valueForKeyPath:@"arrCDrugEntity"];
NSLog(@" client Id : %@", [[items objectAtIndex:1] objectForKey:@"clientId"]);

May this help you

Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72
  • You should not use `...WithContentsOfURL:` when dealing with data from the internet. As it runs synchronous, it will block your main thread and no user interaction will be possible until all of the data has been downloaded. It is really bad practice. And you're mixing up the native JSON implementation in iOS (`NSJSONSerialization`) with code of a 3rd party library (`SBJSON`, a `NSString` does not have a method named `JSONValue`) – Björn Kaiser Jan 03 '13 at 16:51
1

You can use NSJSONSerialization for doing this.

NSData *response = [yourJSONString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: response options: NSJSONReadingMutableContainers error: &err];
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
1

First put your JSON through a JSON formatter before pasting it, easier to read, use this website

http://jsonformatter.curiousconcept.com/

Secondly, find good JSON parser, personally I use SBJSON, found here

http://stig.github.com/json-framework/

Once you have that downloaded, quite easy to parse, like this, example below

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"drugData" ofType:@"json"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
NSString *responseString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];
 NSDictionary *MainJSON = [responseString JSONValue];

NSArray *array = [MainJSON valueForKey:@"arrCDrugEntity"];

for(int i = 0; i < [array count]; i++)
    {
        NSDictionary *temp = [array objectAtIndex:i];
        NSLog(@"%@", [temp valueForKey:@"Drug"]);
    }

Edit: Updated loop, better way of parsing it, as means you can loop through each individual drug object, so easier if you want to parse the data into a drug object class if you need to

AdamM
  • 4,400
  • 5
  • 49
  • 95
1

Use this code

#import "JSONKit.h"

 NSDictionary *dictionary = [stringData objectFromJSONString];
 NSArray *arrayOfDrugs=[NSArray alloc] initWithArray:[dictionary valueForKey:@"arrCDrugEntity"];

 for (NSDictionary *drugDic in arrayOfDrugs)
 {
  NSLog(@"drug id is :%@",[drugDic valueForKey:@"DrugID"]);
  NSLog(@"drug is :%@",[drugDic valueForKey:@"Drug"]);
  NSLog(@"Quantity is :%@",[drugDic valueForKey:@"Quantity"]);
  NSLog(@"Comment is :%@",[drugDic valueForKey:@"Comment"]);
  NSLog(@"FunctionStatus is :%i",[[drugDic valueForKey:@"FunctionStatus"] intValue]);
  NSLog(@"ResultString is :%@",[drugDic valueForKey:@"ResultString"]);
  NSLog(@"ErrorString is %@",[drugDic valueForKey:@"ErrorString"]);
 }

with simple JSON Files but you have to disable ARC in build setting for JSON file.

Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90
1

Import SBJson.h in your class and use JSONValue method for converting json string into dictionary.

NSDictionary *dict = [yourJsonString JSONValue];
    NSArray *arr =  [dict valueForKey:@"arrCDrugEntity"];
    NSMutableArray *drugArray = [[NSMutableArray alloc] init];
    for (NSDictionary *drug in arr) {
        [drugArray addObject:[drug valueForKey:@"Drug"]];
    }
    NSLog(@"drugArray:%@",drugArray);

I think it will be helpful to you.

Prasad G
  • 6,702
  • 7
  • 42
  • 65