如何将json响应数据分成多个字符串?

时间:2015-05-13 10:08:27

标签: ios json string

我想以单独的字符串形式获取JSON响应,这是我的代码

 List<TypedValue> xdata = new List<TypedValue>();
 xdata.Add(new TypedValue(872, "ACF"));
 DBObject object1= acTrans.GetObject(newDimension.ObjectId, OpenMode.ForWrite) as Dimension;
 ResultBuffer resBuffer = new ResultBuffer(xdata.ToArray());
 object1.XData = resBuffer;  

这就是我的回应。

NSString *myRequestString = [NSString stringWithFormat:@"register=yes&email=%@&fname=%@&lname=%@&password=%@&birthDate=%@&zip=%@",txt_email.text,txt_firstname.text,txt_lastname.text,txt_pass.text,txt_dob.text,txt_adress.text];

// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"URL"]];
  // set Request Type
  [request setHTTPMethod: @"POST"];
// Set content-type
//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
//Log Response
    NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);

我希望我的消息,布尔,etype和其他数据分开。

2 个答案:

答案 0 :(得分:3)

试试..

<强>代码:

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
NSArray *message=[dict objectForKey:@"message"];
BOOL boolean=[[dict  objectForKey:@"boolean"]boolValue];
NSString  *eType=[dict  objectForKey:@"eType"];

注意: returnData是来自webservice的响应数据。

答案 1 :(得分:0)

像这样:

 NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];

 NSDictionary *message = responseDict[@"message"];

 NSString *email = message[@"email"];


 BOOL aBoolean = [responseDict[@"boolean"] boolValue];

 int eType = [responseDict[@"eType"] intValue];