[英]How to parse the data into Json Format in ios?
I Converted NSData to string then I am getting the string like below, Now I want to parse this one. 我将NSData转换为字符串,然后得到如下所示的字符串,现在我想解析此字符串。 If parse with json serilazation I am getting json data nil.
如果用json serilazation解析,我将得到json数据为零。
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><EmployeesLoginMethodResponse xmlns="http://tempuri.org/"><EmployeesLoginMethodResult>[{"sms":"You have logged in successfully!","userId":"29","type":"1","name":"mng 56 78"}]</EmployeesLoginMethodResult></EmployeesLoginMethodResponse></soap:Body></soap:Envelope>
If I parse using XML I am Getting The String Like below,In this How to get value of sms , userId , name 如果我使用XML解析,我将得到如下所示的字符串 ,在此方法中,如何获取sms , userId , name的值
[{
"sms": "You have logged in successfully!",
"userId": "13",
"type": "1",
"name": "Suhashini Kumari Singh"
}]
Here is my code 这是我的代码
NSString *urlString=[NSString stringWithFormat:@"http://workforce.wifisocial.in/WebServicesMethods/EmployeesWebService.asmx"];
NSString* webStringURL = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString* requestBody =[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><EmployeesLoginMethod xmlns=\"http://tempuri.org/\"><username>\%@</username><password>\%@</password><IpAddress>\%@</IpAddress><deviceName>\%@</deviceName></EmployeesLoginMethod></soap:Body></soap:Envelope>",self.userNameTextFiled.text,self.passwordTextField.text,ipAddress,deviceName];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
[request setValue:@"\"http://tempuri.org/EmployeesLoginMethod\"" forHTTPHeaderField:@"SOAPAction"];
[request setHTTPBody:[requestBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]] ;
NSError *error;
NSHTTPURLResponse *response = nil;
NSData * urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code: %ld", (long)[response statusCode]);
NSString* responseString = [NSString stringWithUTF8String:[urlData bytes]];
NSLog(@"%@",responseString);
if (urlData)
{
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:urlData options:0 error:&error];
Please try XMLDictionary library. 请尝试XMLDictionary库。 For more reference https://github.com/nicklockwood/XMLDictionary
有关更多参考, 请参见https://github.com/nicklockwood/XMLDictionary
Just convert your NSData to NSDictionary using XMLDictionary as below 只需使用XMLDictionary将NSData转换为NSDictionary,如下所示
NSDictionary *xmlDictionary = [NSDictionary dictionaryWithXMLData:returnData];
If you got you response in json string then try like below, 如果您在json字符串中得到响应,请尝试如下所示,
NSError *jsonError;
NSData *objectData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
here responseString
is final output json string that you have posted in question. 这里
responseString
是您有问题的最终输出json字符串。
Then fetch data from json dictionary
. 然后从
json dictionary
获取数据。
If this scenario will not work then take a look at NSXMLParser , you can refer Appcoda's tutorial . 如果这种情况不起作用,请查看NSXMLParser ,您可以参考Appcoda的教程 。
You can use XML to JSON converter (XMLReader) : https://github.com/amarcadet/XMLReader 您可以使用XML到JSON转换器(XMLReader): https : //github.com/amarcadet/XMLReader
#import "XMLReader.h"
Here is the sample snippet for your code : 这是您的代码的示例片段:
NSData * urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (urlData)
{
NSDictionary *dict = [XMLReader dictionaryForXMLData:urlData error:&error];
NSLog(@"-----%@-----",dict);
NSError *jsonError;
NSString *json2 = [[[[[[dict valueForKey:@"soap:Envelope"] valueForKey:@"soap:Body"] valueForKey:@"EmployeesLoginMethodResponse"] valueForKey:@"EmployeesLoginMethodResult"] valueForKey:@"text"] stringByReplacingOccurrencesOfString:@"\"" withString:@"\""];
NSData *objectData1 = [json2 dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json1 = [NSJSONSerialization JSONObjectWithData:objectData1 options:NSJSONReadingMutableContainers error:&jsonError];
NSLog(@"-----%@-----",json1);
}
It's nil
because it's not the JSON parsing that's failing but because of the conditional type cast of the resulting object as a dictionary. 这是
nil
因为失败不是不是JSON解析,而是因为结果对象的条件类型转换为字典。
It's an array with one item which is dictionary. 这是一个只有一项的字典数组。 So, during parsing cast it as a
NSArray
. 因此,在解析过程中将其
NSArray
为NSArray
。
Like, 喜欢,
Instead of: 代替:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:urlData options:0 error:&error];
Use: 采用:
NSArray *arrJson=[NSJSONSerialization JSONObjectWithData:urlData options:0 error:&error];
NSDictionary *json = [arrJson objectAtIndex:0];
NSString *sms=[json valueForKey:@"sms"];
NSString *userId=[json valueForKey:@"userId"];
NSString *type=[json valueForKey:@"type"];
NSString *name=[json valueForKey:@"name"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.