简体   繁体   中英

how to get array response from json in string?

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...

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"];


     }

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"
...

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;
}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM