1

I am getting the response from json like this

 parcel =     (
                {
            datetime = "2015-08-31 21:48:45";
            height = 2;
            id = 21;
            invoice = NO;
            length = 2;
            mtype = IN;
            "num_item" = 2;
            "parcel_number" = 1;
            pname = "Parcel number - 1";
            "pro_number" = tanu;
            status = 1;
            type = Exact;
            uid = 185;
            weight = 2;
            width = 2;
            wtype = KG;
        }
    );

I want to get the height,date time,invoice in a string.. How can this be done? AnyOne can help me for this...

0

4 Answers 4

3
NSArray * arr_Parcel = [[NSArray alloc]initWithArray:data];
NSMutableDictionary * temp_Dict = [[NSMutableDictionary alloc]init];
for (int i = 0 ; i < arr_Parcel.count; i++)
    {
        temp_Dict = [arr_Parcel objectAtIndex:i];
         NSString * strHeight = [temp_Dict objectForKey:"height"];
         NSString * strdate = [temp_Dict objectForKey:"datetime"];


     }
Sign up to request clarification or add additional context in comments.

Comments

1

If you are using Swift, use SwiftJSON to work with JSON. It's easy to use. For example:

var jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let json = JSON(data: jsonData)
print(json["height"].double) // 2
print(json["datatime"].string) // "2015-08-31 21:48:45"
...

1 Comment

I am using objective C
1

Create a NSObject Class and pass the NSDictionary the make a model class.

In your ViewController Class:

" parcel" is your Array

ModelClass * modelClass = [ModelClass alloc] initWithNewsDictionary: parcel[0]];

Create needed variables in ModelClass.h Class.

-(id)initWithNewsDictionary:(NSDictionary *)dictionary
{
    self = [super init];
    if (self) {
        self.height     = dictionary[@"height"];
        self.datetime   = dictionary[@"datetime"];
        self.invoice    = dictionary[@"invoice"];
        self.weight     = dictionary[@"weight"];
    }
    return self;
}

Comments

0

Try this :-

   NSString *height=[NSString stringWithFormat:@"%@",[[[YourJson valueForKey:@"parcel"] objectAtIndex:0] objectForKey:@"height"]];
   NSString *dateTime=[NSString stringWithFormat:@"%@",[[[YourJson valueForKey:@"parcel"] objectAtIndex:0] objectForKey:@"datetime"]];
   NSString *invoice=[NSString stringWithFormat:@"%@",[[[YourJson valueForKey:@"parcel"] objectAtIndex:0] objectForKey:@"invoice"]];
   NSLog(@"height : %@ , date=%@ ,invoice=%@",height,dateTime,invoice);

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.